先放一下之前c++的BFS,然后试着拿php写一下
博客专区 > 侯禹 的博客 > 博客详情
先放一下之前c++的BFS,然后试着拿php写一下
侯禹 发表于4年前
先放一下之前c++的BFS,然后试着拿php写一下
  • 发表于 4年前
  • 阅读 35
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

 

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.  

Is an escape possible? If yes, how long will it take?  

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).  
L is the number of levels making up the dungeon.  
R and C are the number of rows and columns making up the plan of each level.  
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form  

Escaped in x minute(s).


where x is replaced by the shortest time it takes to escape.  
If it is not possible to escape, print the line  

Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

其实就是个三维的宽搜,小弟代码写的,不好勿喷。。。。。。。

 

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
struct point
{
  int x,y,z;
};
queue <point>que;
    int l,r,c;
point start,end,cur,pre;
int ans;
int xx,yy,zz;
char chess[32][32][32];
int ch[32][32][32];
int visit[32][32][32];
void path(int x,int y,int z,int t)
{
    switch(t)
    {
        case 0:xx=x+1;yy=y;zz=z;break;
        case 1:xx=x;yy=y+1;zz=z;break;
        case 2:xx=x;yy=y;zz=z+1;break;
        case 3:xx=x-1;yy=y;zz=z;break;
        case 4:xx=x;yy=y-1;zz=z;break;
        case 5:xx=x;yy=y;zz=z-1;break;
    }
    return ;
}
int bfs()
{
    que.push(start);
    visit[start.z][start.y][start.x]=1;
    while(!que.empty())
    {
        cur=que.front();
        que.pop();
        if(cur.x==end.x&&cur.y==end.y&&cur.z==end.z)
        {
            return visit[end.z][end.y][end.x];
        }
        for(int i=0;i<6;i++)
        {
            path(cur.x,cur.y,cur.z,i);
if(xx>=0&&yy>=0&&zz>=0&&zz<l&&xx<c&&yy<r&&visit[zz][yy][xx]==0&&ch[zz][yy][xx]==1)
            {
//cout<<"okz:"<<zz<<"oky:"<<yy<<"okx:"<<xx<<endl;
                pre.x=xx;
                pre.y=yy;
                pre.z=zz;
                visit[zz][yy][xx]=visit[cur.z][cur.y][cur.x]+1;
                que.push(pre);
                 }
        }

    }
return 0;
}
int main(void)
{//freopen("r.txt","r",stdin);
    while(cin>>l>>r>>c&&l+r+c!=0)
    {
        memset(chess,0,sizeof(chess));
        memset(ch,0,sizeof(ch));
        memset(visit,0,sizeof(visit));
        for(int i=0;i<l;i++)
        {
            for(int j=0;j<r;j++)
            {
                cin>>chess[i][j];
                for(int k=0;k<c;k++)
                {
                    if(chess[i][j][k]=='.')
                    ch[i][j][k]=1;
                    else if(chess[i][j][k]=='#')
                    ch[i][j][k]=0;
                   else if(chess[i][j][k]=='S')
                   {ch[i][j][k]=1;
                        start.x=k;
                        start.y=j;
                        start.z=i;
                    }
                    else if(chess[i][j][k]=='E')
                   {ch[i][j][k]=1;
                        end.x=k;
                        end.y=j;
                        end.z=i;
                    }
                }
            }
 
        }
        //cout<<"xyz"<<end.x<<end.y<<end.z<<endl;
        int t=bfs();
        while(!que.empty())que.pop();
        if(t!=0)
        cout<<"Escaped in "<<t-1<<" minute(s)."<<endl;
            else
            cout<<"Trapped!"<<endl;

    }
 
}


标签: ACM BFS
共有 人打赏支持
粉丝 93
博文 49
码字总数 34362
×
侯禹
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: