POJ 3083 BFS+DFS
来源:互联网 发布:lex js语法解析 编辑:程序博客网 时间:2024/05/19 23:53
题意真让人迷糊,尤其是方向半天没有搞明白,题目中这两句话(output on a single line the number of (not necessarily unique(可以重复访问某一点)) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.)意思是
(1)优先沿着左边(左上右下的顺时针次序 相对与当前方向)根据上一步的方向来判断当前位置的走向
(2) 优先沿着右边(右上左下的逆时针次序 相对于当前方向)
(3)寻找最点路径(BFS)
大致题意就是这样!
大牛写的代码,简洁漂亮http://blog.csdn.net/wahaha1_/article/details/8664765
#include<stdio.h>#include<string.h>int n,m;int r[4][2]= {{0,-1},{1,0},{0,1},{-1,0}};int l[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};int vis[50][50];char map[50][50];int sx,sy,ex,ey,ans;int que[2500][2];int dfs1(int x,int y,int step){ if(x==ex&&y==ey) return step+1; if(x<0||x>=n||y<0||y>=m||map[x][y]=='#') return 0; ans=(ans+3)%4; int temp=0; while(1) { temp=dfs1(x+l[ans][0],y+l[ans][1],step+1); if(temp>0) break; ans=(ans+1)%4; } return temp;}int dfs2(int x,int y,int step){ if(x==ex&&y==ey) return step+1; if(x<0||x>=n||y<0||y>=m||map[x][y]=='#') return 0; ans=(ans+3)%4; int temp=0; while(1) { temp=dfs2(x+r[ans][0],y+r[ans][1],step+1); if(temp>0) break; ans=(ans+1)%4; } return temp;}int bfs(){ int fir=0,sec=0; que[sec][0]=sx; que[sec++][1]=sy; vis[sx][sy]=1; int step=1; while(fir<sec&&!vis[ex][ey]) { int tmp=sec; step++; while(fir<tmp&&!vis[ex][ey]) { int x=que[fir][0]; int y=que[fir++][1]; for(int i=0; i<4; i++) { int x1=x+r[i][0]; int y1=y+r[i][1]; if(x1>=0&&x1<n&&y1>=0&&y1<m&&!vis[x1][y1]&&map[x1][y1]!='#') { que[sec][0]=x1; que[sec++][1]=y1; vis[x1][y1]=1; } } } } return step;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&m,&n); memset(que,0,sizeof(que)); memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) { scanf("%s",map[i]); for(int j=0; j<m; j++) { if(map[i][j]=='S') { sx=i; sy=j; } if(map[i][j]=='E') { ex=i; ey=j; } } } ans=0; printf("%d",dfs1(sx,sy,0)); ans=0; printf(" %d",dfs2(sx,sy,0)); printf(" %d\n",bfs()); } return 0;}
对上面代码加了注解:
#include<stdio.h>#include<string.h>int n,m;int r[4][2]= {{0,-1},{1,0},{0,1},{-1,0}};//逆时针int l[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};//顺时针int vis[50][50];char map[50][50];int sx,sy,ex,ey,ans;int que[2500][2];int dfs1(int x,int y,int step){ if(x==ex&&y==ey) return step+1; if(x<0||x>=n||y<0||y>=m||map[x][y]=='#') return 0; ans=(ans+3)%4;//相对当前走向,优先沿着左边 int temp=0; while(1)//判断当前位置的走向 { temp=dfs1(x+l[ans][0],y+l[ans][1],step+1); if(temp>0) break; ans=(ans+1)%4; } return temp;}int dfs2(int x,int y,int step){ if(x==ex&&y==ey) return step+1; if(x<0||x>=n||y<0||y>=m||map[x][y]=='#') return 0; ans=(ans+3)%4; int temp=0; while(1) { temp=dfs2(x+r[ans][0],y+r[ans][1],step+1); if(temp>0) break; ans=(ans+1)%4; } return temp;}int bfs(){ int fir=0,sec=0; que[sec][0]=sx; que[sec++][1]=sy; vis[sx][sy]=1; int step=1; while(fir<sec&&!vis[ex][ey]) { int tmp=sec; step++; while(fir<tmp&&!vis[ex][ey]) { int x=que[fir][0]; int y=que[fir++][1]; for(int i=0; i<4; i++) { int x1=x+r[i][0]; int y1=y+r[i][1]; if(x1>=0&&x1<n&&y1>=0&&y1<m&&!vis[x1][y1]&&map[x1][y1]!='#') { que[sec][0]=x1; que[sec++][1]=y1; vis[x1][y1]=1; } } } } return step;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&m,&n); memset(que,0,sizeof(que)); memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) { scanf("%s",map[i]); for(int j=0; j<m; j++) { if(map[i][j]=='S') { sx=i; sy=j; } if(map[i][j]=='E') { ex=i; ey=j; } } } ans=0; printf("%d",dfs1(sx,sy,0)); ans=0; printf(" %d",dfs2(sx,sy,0)); printf(" %d\n",bfs()); } return 0;}
0 0
- poj 3083 DFS+DFS+BFS
- POJ 3083 BFS+DFS
- poj 3083 dfs+bfs
- poj 3083 bfs+dfs
- poj 3083 DFS+BFS
- poj 3083 搜索 DFS,BFS
- POJ 3083图搜 bfs+dfs
- POJ 3083(DFS+BFS)
- POJ 3083 (bfs + dfs)
- POJ 3083 BFS+DFS 40行
- poj 3083 dfs+bfs最短路
- POJ 3083 - 变向DFS + BFS
- POJ 1062 BFS+DFS
- poj 1979DFS&&BFS
- POJ 2308 DFS+BFS
- POJ 1979 简单dfs||bfs
- POJ 1979 3669 DFS BFS
- POJ 3083 Children of the Candy Corn dfs+bfs
- Symmetric Tree
- studio导入libraries包
- 虚函数与多态
- Microsoft.Jet.OleDB.4.0
- Same Tree
- POJ 3083 BFS+DFS
- excel Sheet Column Number
- windows7 内部版本7601 此windows副本不是正版 ---解决方案
- Merge Sorted Array
- Linux 任务调度策略
- IFeatureClassManage接口更新图层全图显示时的范围
- (编译原理)C++源代码单词扫描程序(词法分析)
- Remove Duplicates from Sorted List
- #295 (div.2) E.Pluses everywhere