首页 > 代码库 > poj3984(经典dfs)
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 100010using 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();}
poj3984(经典dfs)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。