HDOJ搜索专题之胜利大逃亡
2012-05-16 17:21
330 查看
BFS题,走三维迷宫。
View Code
View Code
#include <stdio.h> #include <string.h> #include <queue> #define N 50 using namespace std; typedef struct node { int x,y,z; }node; queue<node> Q; node cur,next; int dx[6]={1,-1,0,0,0,0}; int dy[6]={0,0,1,-1,0,0}; int dz[6]={0,0,0,0,1,-1}; int g ,t ; int a,b,c,tmax,ans; void bfs() { bool success=false; memset(t,0xff,sizeof(t)); cur.x=cur.y=cur.z=0; t[0][0][0]=0; while(!Q.empty()) Q.pop(); Q.push(cur); while(!Q.empty() && !success) { cur=Q.front(),Q.pop(); ans=t[cur.x][cur.y][cur.z]; if(cur.x==a-1 && cur.y==b-1 && cur.z==c-1 && ans<=tmax) success=true; for(int d=0;!success && d<6;d++) { next=cur; next.x+=dx[d]; next.y+=dy[d]; next.z+=dz[d]; if(next.x<0 || next.x>=a) continue; if(next.y<0 || next.y>=b) continue; if(next.z<0 || next.z>=c) continue; if(g[next.x][next.y][next.z]) continue; if(t[next.x][next.y][next.z]!=-1) continue; ans=t[next.x][next.y][next.z]=t[cur.x][cur.y][cur.z]+1; if(next.x==a-1 && next.y==b-1 && next.z==c-1 && ans<=tmax) success=true; else if(t[next.x][next.y][next.z]<tmax) Q.push(next); } } if(success) printf("%d\n",ans); else puts("-1"); } int main() { int T,i,j,k; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&a,&b,&c,&tmax); for(i=0;i<a;i++) { for(j=0;j<b;j++) { for(k=0;k<c;k++) scanf("%d",&g[i][j][k]); } } bfs(); } return 0; }
相关文章推荐
- HDOJ搜索专题之Catch That Cow
- HDOJ搜索专题之翻纸牌游戏
- HDOJ搜索专题之Robot Motion
- HDOJ搜索专题之Another Eight Puzzle
- HDOJ搜索专题之Prime Ring Problem
- HDOJ搜索专题之Sticks
- HDOJ搜索专题之变形课
- HDOJ搜索专题之Square
- 搜索专题HDOJ1016输出环
- HDOJ搜索专题之A strange lift
- HDOJ搜索专题之Accept Necklace
- 搜索专题(BFS)HDU 1253-胜利大逃亡
- HDOJ搜索专题之下沙小面的(2)
- HDOJ搜索专题之Kill the monster
- HDOJ搜索专题之Red and Black
- HDOJ搜索专题之Jugs
- HDOJ搜索专题之Counting Sheep
- HDOJ搜索专题之非常可乐
- HDOJ P1253 胜利大逃亡 BFS+简单剪枝
- HDOJ1175连连看搜索