码迷,mamicode.com
首页 > 其他好文 > 详细

poj3984(经典dfs)

时间:2014-12-16 19:09:07      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   sp   for   

 

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

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

bubuko.com,布布扣
#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)\n",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

 

poj3984(经典dfs)

标签:style   blog   http   ar   io   color   os   sp   for   

原文地址:http://www.cnblogs.com/lienus/p/4167691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!