POJ2251---Dungeon Master---BFS最短路之三維迷宮

來源:互聯網
上載者:User

題目連結:http://poj.org/problem?id=2251
題目大意:這個題是BFS的典型模板題,只不過有趣的是它將圖形由平面變為了三維立體圖形,
自認為痛點在輸入上,採用三維字元數組,三層迴圈,最外層迴圈對應三維數組的第三維表示的就是層數。
具體代碼如下:

#include <iostream>#include <queue>#include <cstring>#include <cstdio>using namespace std;const int MAXN=50;char map[MAXN][MAXN][MAXN];//三維字元數組int vis[MAXN][MAXN][MAXN];//標記是否訪問過char str[MAXN];int dirx[6]={-1,1,0,0,0,0};//可以往6個方向移動int diry[6]={0,0,-1,1,0,0};int dirz[6]={0,0,0,0,-1,1};int xru,yru,zru,xcu,ycu,zcu;//入口和出口的資訊int lev,row,col;typedef struct Node  //點的資訊{    int x;    int y;    int z;    int step;}Node;queue <Node> Q;void BFS(){    vis[xru][yru][zru]=1;    Node node,t;    node.x=xru;    node.y=yru;    node.z=zru;    node.step=0;    while(!Q.empty())//重要    {        Q.pop();    }    Q.push(node);    while(!Q.empty())    {        node=Q.front();        Q.pop();        for(int i=0;i<6;i++)        {            t=node;            t.x+=dirx[i];            t.y+=diry[i];            t.z+=dirz[i];            t.step++;            if(t.x==xcu&&t.y==ycu&&t.z==zcu)            {                printf("Escaped in %d minute(s).\n",t.step);                return;            }            if(t.x<0||t.x>=row||t.y<0||t.y>=col||t.z<0||t.z>=lev||map[t.x][t.y][t.z]=='#'||vis[t.x][t.y][t.z])//不符合要求的判斷有三個方向上的邊界,map為石頭,已經訪問的點            {                continue;            }               else            {                vis[t.x][t.y][t.z]=1;//否則標記訪問,壓入隊列中                Q.push(t);            }        }    }    printf("Trapped!\n");    return ;}int main(){    //freopen("input.txt", "r", stdin);    //freopen("output.txt", "w", stdout);    while(scanf("%d%d%d",&lev,&row,&col)!=EOF)    {        memset(map,0,sizeof(map));        memset(str,0,sizeof(str));        memset(vis,0,sizeof(vis));        if(lev==0&&row==0&&col==0)            break;        for(int i=0;i<lev;i++)//外層迴圈展示層數,對應三維數組的第三維            for(int j=0;j<row;j++)//第二層迴圈表示行數,對應三維數組的第一維            {                scanf("%s",str);//按層輸入                for(int k=0;k<col;k++)//第三層迴圈表示列數,對應三維數組的第二維                {                    map[j][k][i]=str[k];                }            }        for(int i=0;i<lev;i++)            for(int j=0;j<row;j++)                for(int k=0;k<col;k++)//找到入口和出口                {                    if(map[j][k][i]=='S')                    {                        xru=j;                        yru=k;                        zru=i;                    }                    if(map[j][k][i]=='E')                    {                        xcu=j;                        ycu=k;                        zcu=i;                    }                }        BFS();      }    return 0;}

僅代表個人觀點,歡迎交流探討,勿噴。

PhotoBy:WLOP

http://weibo.com/wlop

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.