#include <bits/stdc++.h> using namespace std;

queue < int > qz, qx, qy, qt; int l, r, c, e1, e2, e3; char a[50][50][50]={}; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}, dz[2] = {1, -1}; void bfs() { while(!qx.empty()) { int x, y, z, t; x = qx.front(); y = qy.front(); z = qz.front(); t = qt.front(); qz.pop(); qx.pop(); qy.pop(); qt.pop(); a[x][y][z] = '#'; for(int i = 0; i < 4; i++) { int tx = x + dx[i]; int ty = y + dy[i]; if(a[z][tx][ty] == '.') { qz.push(z); qx.push(tx); qy.push(ty); qt.push(t + 1); a[z][tx][ty] = '#'; if(z == e1 && tx == e2 && ty == e3) { cout << "Escaped in " << t + 1 << " minute(s).\n"; return; } } } for(int i = 0; i < 2; i++) { int tz = z + dz[i]; if(a[tz][x][y] == '.') { qz.push(tz); qx.push(x); qy.push(y); qt.push(t + 1); a[tz][x][y] = '#'; if(tz == e1 && x == e2 && y == e3) { cout << "Escaped in " << t + 1 << " minute(s).\n"; return; } } } } cout << "Trapped!\n"; return; }

int main() { while(1) { cin >> l >> r >> c; if(l == 0) break; memset(a, '\0', sizeof(a)); while(!qx.empty()) qx.pop(); while(!qy.empty()) qy.pop(); while(!qz.empty()) qz.pop(); while(!qt.empty()) qt.pop(); for(int i = 1; i <= l; i++) { for(int j = 1; j <= r; j++) { for(int k = 1; k <= c; k++) { cin >> a[i][j][k]; if(a[i][j][k] == 'S') { qz.push(i); qx.push(j); qy.push(k); qt.push(0); } if(a[i][j][k] == 'E') { e1 = i; e2 = j; e3 = k; a[i][j][k] = '.'; } } } } bfs(); } return 0; }

3 条评论

  • @ 2024-8-16 15:52:58

    • @ 2024-7-18 10:53:26
      #include<bits/stdc++.h>
      using namespace std;
      int a[105][105][105],n,m,o;//数据较水,直接开三维数组 
      char p;
      int dx[15]={0,0,0,0,1,-1};/*三个方向的方向数组,一共上下左右前后六个拓展维度*/ 
      int dy[15]={0,0,1,-1,0,0};
      int dz[15]={1,-1,0,0,0,0};
      struct	kk//结构体用于记录每个点的数据 
      {
      	int x;
      	int y;
      	int z;
      	int step;
      };
      kk	q[1000005];//队列,拒绝STL 
      bool check(int x,int y,int z)// 检验这个点是否越界,是否已经走过 
      {
      	if(x<1 || x>n || y<1 || y>m || z<1 || z>o)
      		return 0;
      	if(a[x][y][z])
      		return 0;
      	return 1;
      }
      void bfs(int x1,int y1,int z1,int x2,int y2,int z2)//bfs函数 
      {
      	int head=1,tail=1;//头尾指针初始化 
      	bool flag=1;//是否有答案 
      	q[1].x=x1;//第一个点入队 
      	q[1].y=y1;
      	q[1].z=z1;
      	q[1].step=0;
      	a[x1][y1][z1]=1;//地图标记 
      	while(head<=tail) 
      	{
      		for(int i=0;i<6;i++)//循环六个方向 
      		{
      			int tx=q[head].x+dx[i];
      			int ty=q[head].y+dy[i];
      			int tz=q[head].z+dz[i];
      			if(check(tx,ty,tz))//检验,check函数见上 
      			{
      				tail++;//符合条件,尾指针++
      				 
      				q[tail].x=tx;
      				q[tail].y=ty;
      				q[tail].z=tz;
      				q[tail].step=q[head].step+1;//新的点入队 
      			
      				a[tx][ty][tz]=1;//地图标记 
      			
      				if(tx==x2 && ty==y2 && tz==z2)//检验是否为终点 
      				{
      					printf("Escaped in %d minute(s).\n",q[tail].step);
      					flag=0;//存在答案,flag=false 
      				}
      			}
      		}
      		head++;//头指针++,切记切记,初学者很容易忘记导致死循环 
      	}
      	if(flag)
      		printf("Trapped!\n");//无答案输出Trapped!
      	return;
      }
      int main()
      {
      	int x1,y1,z1,x2,y2,z2;
      	while(1)//无限循环处理多组数据 
      	{
      		memset(a,0,sizeof(a));//地图清零 
      	
      		cin>>n>>m>>o;//读入终点坐标 
      	
      		if(n==0 && m==0&& o==0)//循环结束条件 
      			break;
      		else
      		{
      			for(int i=1;i<=n;++i)
      				for(int j=1;j<=m;++j)
      					for(int k=1;k<=o;++k)
      					{
      						cin>>p;
      						if(p=='.')
      							a[i][j][k]=0;
      						else if(p=='#')
      							a[i][j][k]=1;
      						else if(p=='S')//记录起点 
      						{
      							a[i][j][k]=0;
      							x1=i;
      							y1=j;
      							z1=k;
      						}
      						else if(p=='E')//记录终点 
      						{
      							a[i][j][k]=0;
      							x2=i;
      							y2=j;
      							z2=k;
      						}
      					}//读入地图 
      			bfs(x1,y1,z1,x2,y2,z2);	//bfs(起点三个坐标,终点三个坐标)	
      		}
      	}
      	return 0;//完美结束!!! 
      }
      
      • @ 2024-7-18 9:56:23

        希丰展,使md

        望丰展,使md

        • 1

        信息

        ID
        9316
        时间
        1000ms
        内存
        256MiB
        难度
        6
        标签
        (无)
        递交数
        129
        已通过
        38
        上传者