ZOJ Problem Set - 1940 Dungeon Master

BFS

简单的!!!!

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node
{
    int l;
    int r;
    int c;
    int t;
};
char ch[31][31][31];
queue<Node> q;
int main()
{
    int l,r,c,i,j,k,x,y,z,t;
    Node tmp,tmp1,end;
    bool flag ;
    //freopen("a.txt","r",stdin);
    while(scanf("%d%d%d",&l,&r,&c)!=EOF)
    {
        if(l ==0&&r==0&&c==0) break;
        flag = 0;
        
        for(i = 0;i < l;i ++)
            for(j = 0;j < r;j ++)
                for(k = 0;k < c;k ++)
                {
                    cin>>ch[i][j][k];
                    if(ch[i][j][k] == 'S')
                    {
                        tmp.l = i;
                        tmp.r = j;
                        tmp.c = k;
                        tmp.t = 0;
                        q.push(tmp);
                        ch[i][j][k] = '#';
                    }
                    if(ch[i][j][k] == 'E')
                    {
                        end.l = i;
                        end.r = j;
                        end.c = k;
                    }

                }
        
        int dir[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
        while(!q.empty())
        {
            tmp = q.front();
            q.pop();
            if(tmp.l==end.l&&tmp.r == end.r&&tmp.c == end.c) {flag = 1;
            printf("Escaped in %d minute(s).\n",tmp.t);
            break;}
            else
            {
                for(i = 0;i < 6;i ++)
                {
                    x = tmp.l + dir[i][0];
                    y = tmp.r + dir[i][1];
                    z = tmp.c + dir[i][2];
                    t = tmp.t + 1;
                    if(x>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c&&ch[x][y][z]!='#'){
                        tmp1.l = x;
                        tmp1.r = y;
                        tmp1.c = z;
                        tmp1.t = t;
                        q.push(tmp1);
                        ch[tmp1.l][tmp1.r][tmp1.c] = '#';
                    }
                }
            }
            
        }
        if(flag == 0) 
        printf("Trapped!\n");
        while(!q.empty()) q.pop();
    }
    return 0;
}


阅读更多
文章标签: c
个人分类: 搜索
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭