POJ 2251 Dungeon Master【BFS】

来源:互联网 发布:输入法 for mac 编辑:程序博客网 时间:2024/05/16 05:58

题目:http://poj.org/problem?id=2251


三维迷宫,x,y坐标相同时可以直接到下一层,BFS

que.push(a);                  队尾进队列,a为进队列元素   
que.pop();                    队首出队列   
list temp=que.front();        队首的元素   
int size=que.size();          元素个数   

while(!que.empty())que.pop(); 重复使用时,初始化清空队列 


#include<stdio.h>#include<queue>#include<string.h>using namespace std;int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}},l,c,r;char map[60][60][60];struct maze{int x,y,level,time;};queue<maze> que;//定义队列int check(int x,int y,int level ){if(x>=0 && x<r && y>=0 && y<c && level>=0 && level<l)return 1;return 0;}int bfs(struct maze start){int i;struct maze cur,next;while(!que.empty())      que.pop(); //清空队列que.push(start);while( !que.empty() ) {cur=que.front();que.pop();for(i=0;i<6;i++){next.x = cur.x + dir[i][0];next.y = cur.y + dir[i][1];next.level = cur.level + dir[i][2];next.time = cur.time + 1 ;if( map[next.level][next.x][next.y]!='#' && check(next.x,next.y,next.level) ){ if( map[next.level][next.x][next.y] == 'E' )return next.time;map[next.level][next.x][next.y]='#';//vist过que.push(next);}}}return 0;}int main(){int i,j,min,k; struct maze start;//freopen("test.txt","r",stdin);while(scanf("%d%d%d",&l,&r,&c) && (l||r||c) ){getchar();//这题输入格式真坑爹,用cin>>的话方便多min=0;for(k=0;k<l;k++){for(i=0;i<r;i++){for(j=0;j<c;j++){scanf("%c",&map[k][i][j]);if(map[k][i][j] == 'S'){start.x=i;start.y=j;start.time=0;start.level=k;map[k][i][j]='#';}}getchar();}getchar();}min=bfs(start);if(min==0)printf("Trapped!\n");elseprintf("Escaped in %d minute(s).\n",min);}return 0;}


0 0
原创粉丝点击