Dungeon Master([kuangbin带你飞]专题一 简单搜索)

题目大意:题目链接

给个多层的迷宫,求从起点到终点的最短路径,不能到达就输出Trapped!

解体思路:

bfs直接搜,没什么好说的,一开始忘记清空队列一只WA,果然我还是菜鸡



#include
#include
#include
#include
#include
#include
using namespace std;
struct node{
	int x,y,z;
}no;
queue q;
int ans=0;
const int maxn = 35;
char mp[maxn][maxn][maxn];
int xx[6] = {-1,1,0,0,0,0};
int yy[6] = {0,0,-1,1,0,0};
int zz[6] = {0,0,0,0,-1,1};
bool vis[maxn][maxn][maxn];
int len[maxn][maxn][maxn];
int l,r,c;
int bx,by,bz;



int bfs(){
    while(!q.empty()) q.pop();
	memset(vis,false,sizeof(vis));
	memset(len,0,sizeof(len));
    no.x=bx;
    no.y=by;
    no.z=bz;
    q.push(no);
    len[no.x][no.y][no.z]=0;
    while(!q.empty()){
        node no1=q.front();
        q.pop();
        for(int i=0;i<6;i++){
            int dx=no1.x+xx[i];
            int dy=no1.y+yy[i];
            int dz=no1.z+zz[i];

            if(!vis[dx][dy][dz] && (mp[dx][dy][dz]=='.' || mp[dx][dy][dz]=='E')
                && dx>=0 && dx = 0 && dy< r && dz >=0 && dz < c)
			{
            	
                vis[dx][dy][dz]=1;
                len[dx][dy][dz]=len[no1.x][no1.y][no1.z]+1;
                no.x=dx;
                no.y=dy;
                no.z=dz;
                q.push(no);
                if(mp[no.x][no.y][no.z]=='E')
                {
                    return len[no.x][no.y][no.z];
                }
            }
        }
    }
    return 0;


}




int main(int argc, char const *argv[])
{
	//freopen("t.txt","r",stdin);

	while(~scanf("%d%d%d",&l,&r,&c)){
		if(l==0 && r==0 && c==0) break;
		ans=0;

		for(int x=0;x>mp[x][y][z];
					if(mp[x][y][z]=='S'){
						bx=x;by=y;bz=z;
					}
				}
			}
		}
		int ans=bfs();
		if(ans) printf("Escaped in %d minute(s).\n",ans);
		else printf("Trapped!\n");
	}
	return 0;
}



你可能感兴趣的:(简单搜索,poj)