ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

bfs的三维棋盘

2021-07-04 21:00:23  阅读:193  来源: 互联网

标签:ch tz tx ty int 32 三维 bfs 棋盘


题目:

Dungeon Master

poj2251

https://vjudge.net/problem/POJ-2251

#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
int l,r,c;
char ch[32][32][32];
int d[32][32][32];
int xx[7]={1,-1,0,0,0,0};
int yy[7]={0,0,1,-1,0,0};
int zz[7]={0,0,0,0,1,-1};
struct node
{
    int x,y,z;
};
queue<node> q;
int bfs(node st)
{
    while(!q.empty())
   q.pop();
   d[st.x][st.y][st.z]=0;
   q.push(st);
   while(!q.empty())
   { node te=q.front();
   q.pop();

         int x=te.x,y=te.y,z=te.z;
    ch[x][y][z]='#';
    for(int i=0;i<6;i++)
    {
        int tx=x+xx[i];
        int ty=y+yy[i];
        int tz=z+zz[i];
      if(tx<0||tx>=l||ty<0||ty>=r||tz<0||tz>=c)
        continue;
      if(ch[tx][ty][tz]=='#')
        continue;
      d[tx][ty][tz]=d[x][y][z]+1;
      if(ch[tx][ty][tz]=='E')
        return d[tx][ty][tz];
      node tt;
      tt.x=tx;
      tt.y=ty;
      tt.z=tz;
      ch[tx][ty][tz]='#';
      q.push(tt);
    }
   }
return -1;
}
int main()
{
    while(1)
    { int stx,sty,stz;
        scanf("%d %d %d",&l,&r,&c);
        if(l==0&&r==0&&c==0)
break;

        for(int i=0;i<l;i++)
         for(int j=0;j<r;j++)
         {
             getchar();
             scanf("%s",ch[i][j]);
             for(int k=0;k<strlen(ch[i][j]);k++)
             {
                 if(ch[i][j][k]=='S')
                 {
                     stx=i;
                     sty=j;
                     stz=k;

                 }
             }
         }
      node st;
      st.x=stx;
      st.y=sty;
      st.z=stz;
    int ans=bfs(st);
    if(ans==-1)
{
printf("Trapped!\n");
}
else
{
    printf("Escaped in %d minute(s).\n",ans);
}
    }
}

 

标签:ch,tz,tx,ty,int,32,三维,bfs,棋盘
来源: https://www.cnblogs.com/aacm/p/14969862.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有