hdu~1253(bfs)
2014-11-20 19:25
169 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1253
简单bfs,就是多了上下两个方向,用bfs做的话,好像一旦到了终点,这条路就是最短的。
注意seat【】【】【】中第一维存的是Z坐标,
不过由于此题起点为0 0 0,终点为a-1,b-1,c-1(我在题目中改成1,1,1到a,b,c)。所以这个没影响。
简单bfs,就是多了上下两个方向,用bfs做的话,好像一旦到了终点,这条路就是最短的。
注意seat【】【】【】中第一维存的是Z坐标,
不过由于此题起点为0 0 0,终点为a-1,b-1,c-1(我在题目中改成1,1,1到a,b,c)。所以这个没影响。
<span style="font-size:18px;">#include <stdio.h> #include <queue> #define MAX 55 int seat[MAX][MAX][MAX];//存图 int a,b,c,t,ans; int dir[10][3]={0,0,0,1,0,0,0,1,0,-1,0,0,0,-1,0,0,0,1,0,0,-1};//三维的,多了两个方向 typedef struct ac { int x,y,z,time; }node; using namespace std; void bfs(int x,int y,int z,int time) { queue <node > q; node next,temp; next.x=x; next.y=y; next.z=z; next.time=time; seat[x][y][z]=1;//走过记为1,(这里的墙也是用1表示的)<span style="white-space:pre"> </span> q.push(next); while(!q.empty()) { next=q.front(); q.pop(); if(next.x==a && next.y==b && next.z==c)//到终点返回 { ans=next.time; return ; } for(int i=1;i<=6;i++) { temp.x=next.x+dir[i][0]; temp.y=next.y+dir[i][1]; temp.z=next.z+dir[i][2]; temp.time=next.time+1; if(temp.x<1 || temp.x>a || temp.y<1 || temp.y>b || temp.z<1 || temp.z>c) continue; if(seat[temp.x][temp.y][temp.z]) continue; seat[temp.x][temp.y][temp.z]=1; q.push(temp); } } } int main() { int i,j,l,T; scanf("%d",&T); while(T--) { scanf("%d %d %d %d",&a,&b,&c,&t); for(i=1;i<=a;i++) for(j=1;j<=b;j++) for(l=1;l<=c;l++) scanf("%d",&seat[i][j][l]); ans=1005; bfs(1,1,1,0); if(ans>=t) //未在规定时间内到达。 printf("-1\n"); else printf("%d\n",ans); } return 0; } </span>
相关文章推荐
- hdu 1253 胜利大逃亡(BFS)
- HDU----1253胜利大逃亡 BFS
- hdu 1253 胜利大逃亡(三维bfs)
- hdu 1253 胜利大逃亡 (BFS)
- hdu 题目1253 胜利大逃亡(BFS)
- hdu 1253:胜利大逃亡(基础广搜BFS)
- HDU 1253 胜利大逃亡 NYOJ 523【BFS】
- HDU1253 胜利大逃亡 dfs,bfs C++
- hdu 1253 胜利大逃亡(bfs)
- hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
- hdu 1253 胜利大逃亡 简单bfs
- HDU 1253 胜利大逃亡 【BFS】
- HDU_1253 胜利大逃亡(BFS)
- HDU_1253——胜利大逃亡,三围空间BFS
- HDU 1253 胜利大逃亡(BFS)
- HDU1253 胜利大逃亡 BFS
- HDU 1253 胜利大逃亡( BFS )
- HDU 1253 胜利大逃亡(三维数组+bfs)
- hdu-1253 胜利大逃亡(BFS)
- HDU 1253 -- 胜利大逃亡 bfs