现在的位置: 首页 > 综合 > 正文

[hoj]Dungeon Master

2018年03月17日 ⁄ 综合 ⁄ 共 1444字 ⁄ 字号 评论关闭

依然BFS。

三维迷宫的竖坐标是第一维!一不小心就写串了~~~

/*This Code is Submitted by Iris for Problem 1448 at 2013-07-24 17:48:59*/
#include <cstdio>
#include <cstring>
#include <queue>
#include <math.h>
using namespace std;
typedef struct point{
    int x,y,z,step;
}point;
int move [6][3] = {{1,0,0},{-1,0,0},{0,0,-1},{0,0,1},{0,1,0},{0,-1,0}};
int main()
{
    char cube[30][30][30];
    int map[30][30][30];
    int l,r,c;
    while(scanf("%d %d %d",&l,&r,&c)&&(l+r+c))
    {
        for(int i=0;i<l;i++)
            for(int j=0;j<r;j++)
                scanf("%s",cube[i][j]);
        point st,en;
        for(int i=0;i<l;i++)
            for(int j=0;j<r;j++)
                for(int k=0;k<c;k++)
                {
                    if(cube[i][j][k]=='#')
                        map[i][j][k] = 1;
                    else if(cube[i][j][k]=='.')
                        map[i][j][k] = 0;
                    else if(cube[i][j][k]=='S')
                    {
                        st.z = i;
                        st.x = j;
                        st.y = k;
                        st.step = 0;
                        map[i][j][k] = 1;
                    }
                    else{
                        en.z = i;
                        en.x = j;
                        en.y = k;
                        map[i][j][k] = 0;
                    }
                }
     //   for(int i=0;i<l;i++)
      //  {
     //       for(int j=0;j<r;j++)
     //       {
     //           for(int k=0;k<c;k++)
     //               printf("%d ",map[i][j][k]);
     //           printf("\n");
     //       }
      //      printf("\n");
     //   }
        queue<point> q;
        q.push(st);
        bool fnd = 0;
        while(!q.empty())
        {
            point p = q.front();
           // printf("pop %d %d %d\n",p.x,p.y,p.z);
            q.pop();
            for(int i=0;i<6;i++)
            {
                int tz = move[i][0] + p.z;
                int tx = move[i][1] + p.x;
                int ty = move[i][2] + p.y;
                if(tx==en.x && ty==en.y && tz==en.z)
                {
                    printf("Escaped in %d minute(s).\n",p.step+1);
                    fnd = 1;
                    break;
                }
       //         if(tx>=0 && tx<r &&
       //            ty>=0 && ty<c &&
        //           tz>=0 && tz<l)
        //            printf("map = %d\n",map[tx][ty][tz]);
                if(tx>=0 && tx<r &&
                   ty>=0 && ty<c &&
                   tz>=0 && tz<l &&
                   !map[tz][tx][ty])
                {

                    point tmp = {tx,ty,tz,p.step+1};
                    q.push(tmp);
                 //   printf("push %d %d %d\n",tx,ty,tz);
                    map[tz][tx][ty] = 1;
                }
            }

            if(fnd==1)
            {
               // printf("out");
                break;
            }


           if(q.empty())
                printf("Trapped!\n");

        }
    }
    return 0;
}
 

抱歉!评论已关闭.