poj 2251 Dungeon Master

來源:互聯網
上載者:User

標籤:

#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;int k,n,m;char s[40][40][40];int use[40][40][40];int dx[]= {0,0,1,-1,0,0};int dy[]= {1,-1,0,0,0,0};int dz[]= {0,0,0,0,1,-1};struct data{    int x,y,z,t;} t1,t0;queue<data>q;int bfs(int sx,int sy,int sz){    int i,j,flag=0;    while(!q.empty()) q.pop();    t0.x=sx;    t0.y=sy;    t0.z=sz;    t0.t=0;    q.push(t0);    while(!q.empty())    {        t1=q.front();        q.pop();        if(s[t1.z][t1.x][t1.y]==‘E‘)        {            flag=t1.t;            break;        }        for(i=0; i<6; i++)        {            t0.x=t1.x+dx[i];            t0.y=t1.y+dy[i];            t0.z=t1.z+dz[i];            t0.t=t1.t+1;            if(t0.x<0||t0.x>=n||t0.y<0||t0.y>=m||t0.z<0||t0.z>=k) continue;            if(s[t0.z][t0.x][t0.y]==‘#‘) continue;            if(t0.t<use[t0.z][t0.x][t0.y]||use[t0.z][t0.x][t0.y]==0)            {                q.push(t0);                use[t0.z][t0.x][t0.y]=t0.t;            }        }    }    return flag;}int main(){    int i,j,p,sx,sy,sz;    while(~scanf("%d%d%d",&k,&n,&m))    {        if(k==0&&n==0&&m==0) break;        //getchar();        for(p=0; p<k; p++)        {            for(i=0; i<n; i++)            {                scanf("%s",s[p][i]);                for(j=0; j<n; j++)                {                    if(s[p][i][j]==‘S‘)                    {                        sx=i;sy=j;sz=p;                    }                }                //getchar();            }            //getchar();        }        memset(use,0,sizeof(use));        int f=bfs(sx,sy,sz);        if(f>0) printf("Escaped in %d minute(s).\n",f);        else printf("Trapped!\n");    }    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

poj 2251 Dungeon Master

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.