您的位置:首页 > 其它

【一周搜索】poj3984迷宫问题 (dfs)

2016-11-26 16:49 232 查看
鉴于最近一周要打搜索的题目,索性也把第一个搜索入门题贴出来。。在实验室憋了一个小时。。敲掉这个入门==图论好难啊

maya= =

感觉是废狗了

题意大概是从(0,0)到(4,4)位置的路径(每个坐标点)

dfs搞搞就可以啦:)

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

#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
using namespace std;
typedef __int64 LL;
LL mapn[5][5];
bool vis[5][5];
struct node{
int x,y;
};
stack <node> p;
void readin(){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cin>>mapn[i][j];
}
}
}
void dfs(int x,int y){
if(!vis[x][y]){
node cur;cur.x=x;cur.y=y;
vis[x][y]=1;p.push(cur);
}
if(x==4&&y==4){
return;
}
if(!vis[x+1][y]&&!mapn[x+1][y])dfs(x+1,y);
else if(!vis[x][y+1]&&!mapn[x][y+1])dfs(x,y+1);
else{
p.pop();
node cur=p.top();
dfs(cur.x,cur.y);
}
}
int main(){
memset(vis,0,sizeof(vis));
readin();
dfs(0,0);
stack<node> q ;
while(!p.empty()){
q.push(p.top());
p.pop();
}
while(!q.empty()){
printf("(%d, %d)\n",q.top().x,q.top().y);
q.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs 一周 搜索 poj cstring