poj2251的代码又改了改~

来源:互联网 发布:cf一直出现网络异常 编辑:程序博客网 时间:2024/06/10 03:16
#include<iostream>using namespace std;#include<queue>#include<cstring>#include<string>char temp;bool vis[50][50][50];bool vist[50][50][50];const int dx[6] = {1,-1,0,0,0,0};const int dy[6] = {0,0,1,-1,0,0};const int dz[6] = {0,0,0,0,-1,1};struct node{    int x,y,z;    int step;};node now;node S,T;int bfs(){    memset(vis,false,sizeof(vis));    queue<node>q;    q.push(S);    while(!q.empty())    {        now = q.front();        q.pop();        for(int i=0; i<6; i++)        {            node New;            New.x = now.x + dx[i];            New.y = now.y + dy[i];            New.z = now.z + dz[i];            New.step = now.step + 1;            if(!vist[New.x][New.y][New.z] || vis[New.x][New.y][New.z])                continue;            q.push(New);            vis[New.x][New.y][New.z] = true;            if(New.x == T.x && New.y == T.y && New.z == T.z)                return New.step;        }    }    return -1;}int main(){    int L, R, C;    while(cin >> L >> R >> C && L)    {        memset(vist,false,sizeof(vist));        for(int i=1; i<=L; i++)        {            for(int j=1; j<=R; j++)            {                for(int k=1; k<=C; k++)                {                    cin >> temp;                    if(temp == 'S')                    {                        vist[i][j][k] = true;                        S.x = i;                        S.y = j;                        S.z = k;                        S.step = 0;                    }                    if(temp == 'E')                    {                        vist[i][j][k] = true;                        T.x = i;                        T.y = j;                        T.z = k;                    }                    if(temp == '.')                    {                        vist[i][j][k] = true;                    }                }            }        }        int ans = bfs();        if (ans<0) cout <<"Trapped!" << endl;        else            cout << "Escaped in " <<ans << " minute(s)." << endl;    }    return 0;}

0 0
原创粉丝点击