POJ 3083 Children of theCandy Corn(DFS and BFS)
来源:互联网 发布:人工智能 感知 编辑:程序博客网 时间:2024/06/05 12:02
POJ 3083 Children of theCandy Corn(DFS and BFS)
http://poj.org/problem?id=3083
题意:
有一个迷宫,要你求分别算出从迷宫起点到终点的3种距离:优先左转,优先右转,以及最短距离。保证左转和右转一定能从起点到达终点的。S是起点,E是终点,“.”是可行格。求三种走法的最短距离?
分析:
又是一道题意模糊的题目。
首先我们要确定虽然题目中没说,但是左转优先的时候如果走不通就往上走,然后才是右转,最后才是往下走。
右转优先的反向顺序是:右,上,左,下。
由于题目保证一直优先左转和一直优先右转一定能从起点到终点,所以在进行这两个DFS的时候我们不需要判重,就算走过重复的点也无所谓。还有一点可以知道这两种走法不会把全图的所有可行点都走一遍,它们走的是边界那圈。
由于题目中保证了上面两种走法一定能走到终点,且需要注意左转优先始终是相对于当前的方向而已的。比如我们令左方向=0,上方向=1,右方向=2,下方向=3的时候。假设当前方向为dir,那么当前方向dir的左方向=(dir+3)%4(自己取个dir的值验证下看看是不是)
另外就算有死胡同,我们这种走法也能原路走出死胡同,并且继续前进,自己用题目中的例子模拟一下看看是不是,在题目中限定了s和e点的位置只能在边界且不在拐角的情况下。
最后题目中求最短路径,直接用BFS即可。
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxr=50;const int maxc=50;int R,C;const int l[4][2]={{0,-1},{-1,0},{0,1},{1,0}};//左,上,右,下const int r[4][2]={{0,1},{-1,0},{0,-1},{1,0}};//右,上,左,下int sx,sy,ex,ey;char maze[maxr][maxc];int dfs_l(int x,int y,int step,int ans)//ans是当前方向{ if(x==ex&&y==ey) return step+1; if(x<0||x>=R || y<0 || y>=C || maze[x][y]=='#') return 0; ans=(ans+3)%4; int temp=0; while(true) { temp= dfs_l(x+l[ans][0],y+l[ans][1],step+1,ans); if(temp) break; ans=(ans+1)%4; } return temp;}int dfs_r(int x,int y,int step,int ans){ if(x==ex&&y==ey) return step+1; if(x<0 || x>=R || y<0 || y>=C || maze[x][y]=='#') return 0; int temp=0; ans=(ans+3)%4; while(true) { temp=dfs_r(x+r[ans][0],y+r[ans][1],step+1,ans); if(temp) break; ans=(ans+1)%4; } return temp;}int vis[maxr][maxc];int dist[maxr][maxc];struct Node{ int x,y; Node(int x,int y):x(x),y(y){}};queue<Node> Q;int BFS(){ Q.push(Node(sx,sy)); memset(vis,0,sizeof(vis)); dist[sx][sy]=1; vis[sx][sy]=1; while(!Q.empty()) { Node node=Q.front();Q.pop(); int x=node.x,y=node.y; for(int d=0;d<4;d++) { int nx=x+r[d][0]; int ny=y+r[d][1]; if(nx>=0&&nx<R&&ny>=0&&ny<C&&maze[nx][ny]=='.'&&vis[nx][ny]==0) { if(nx==ex&&ny==ey) return dist[x][y]+1; vis[nx][ny]=1; dist[nx][ny]=dist[x][y]+1; Q.push(Node(nx,ny)); } } } return -1;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d%*c",&C,&R); for(int i=0;i<R;i++) { for(int j=0;j<C;j++) { scanf("%c",&maze[i][j]); if(maze[i][j]=='S') { sx=i;sy=j; maze[i][j]='.'; } else if(maze[i][j]=='E') { ex=i;ey=j; maze[i][j]='.'; } } getchar();//读回车符 } printf("%d ",dfs_l(sx,sy,0,0));//初始方向虽然可能不是0(向上),但是不影响,可以转向到正确的方向 printf("%d ",dfs_r(sx,sy,0,0)); printf("%d\n",BFS()); } return 0;}
0 0
- POJ 3083 Children of theCandy Corn(DFS and BFS)
- POJ 3083 Children of the Candy Corn dfs+bfs
- poj 3083 Children of the Candy Corn(dfs+bfs)
- POJ 3083Children of the Candy Corn(DFS*2+BFS)
- POJ 3083 Children of the Candy Corn (bfs+dfs)
- POJ 3083:Children of the Candy Corn(DFS+BFS)
- POJ 3083 - Children of the Candy Corn(BFS+DFS)
- POJ 3083 Children of the Candy Corn (DFS + BFS)
- POJ 3083-Children of the Candy Corn(dfs+bfs)
- poj 3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn dfs+bfs
- poj-3083 Children of the Candy Corn-DFS+BFS
- Poj 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083 Children of the Candy Corn(BFS + DFS)
- [Poj 3083] Children of the Candy Corn bfs+dfs
- [POJ 3083]Children of the Candy Corn[DFS+BFS]
- android权限大全
- linux shell awk获得外部变量(变量传值)简介
- VC中的DoDataExchange
- 昨天全国十大高温城市河南占八个 明后两天有降水
- EGL接口介绍
- POJ 3083 Children of theCandy Corn(DFS and BFS)
- OCP 1Z0 051 5
- 一致性 hash 算法( consistent hashing )
- Eclipse上GIT插件EGIT使用手册之九_Rebase和Merge的区别
- pat Data_stucture 表达式转换-栈的应用
- 使用Spring的HibernateDaoSupport时的getSession()3个方法的区别
- Java中Set的使用
- 大家好!女生职场难
- 微软面试100题之第5题