zoukankan      html  css  js  c++  java
  • poj3984(经典dfs)

    题目链接:http://poj.org/problem?id=3984

    分析:直接深搜从起点到终点,如何取最短路线,其实只要优先向下或向右走即可。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define N 100010
    using namespace std;
    int s[10][10];
    stack<int>s1,s2;
    int vis[10][10];
    int judge(int a,int b)
    {
        return a>=0&&a<5&&b>=0&&b<5&&s[a][b]==0&&!vis[a][b];
    }
    int dfs(int x,int y)
    {
        if(x==4&&y==4)
        {
            s1.push(x);s2.push(y);return 1;
        }
        vis[x][y]=1;
        if(judge(x,y+1)&&dfs(x,y+1)||judge(x+1,y)&&dfs(x+1,y)||
           judge(x-1,y)&&dfs(x-1,y)||judge(x,y-1)&&dfs(x,y-1))
        {
    
            s1.push(x);s2.push(y);return 1;
        }
        else
        {
            return 0;
        }
        return 0;
    }
    void print()
    {
        while(!s1.empty())
        {
            printf("(%d, %d)
    ",s1.top(),s2.top());
            s1.pop();s2.pop();
        }
    }
    int main()
    {
        memset(vis,0,sizeof(vis));
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)scanf("%d",&s[i][j]);
        dfs(0,0);print();
    }
    View Code
  • 相关阅读:
    由二进制移位想到的
    KDJ指标详解
    PMP考试结束
    转K线理论初级二
    日本地震效应
    Baseline之流水先生的见解
    KDJ判断原则
    转K线理论初级一
    管理学法则
    今天提到KW,特此@Mark一下
  • 原文地址:https://www.cnblogs.com/lienus/p/4167691.html
Copyright © 2011-2022 走看看