当前位置:网站首页>AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
2022-04-23 05:33:00 【Ice Cream~】
Original link :https://www.acwing.com/problem/content/1098/
subject :
You are now trapped in a three-dimensional dungeon , We need to find the fastest way out !
The dungeon consists of several unit cubes , Some of them do not contain rock obstacles and can pass directly through , Some contain rock obstacles that cannot be passed .
To the north , Southward , To the east , To the West , It takes one minute to move a unit up or down .
You can't move diagonally , The boundary of the maze is hard rock , You can't go beyond the boundary .
Excuse me, , Is it possible for you to escape ?
If possible , How long does it take? ?
Input format
Input contains multiple sets of test data .
The first row of each set of data contains three integers L,R,C Each represents the number of dungeons , And the number of rows and columns in each dungeon .
Next is L individual R That's ok C The character matrix of the column , Used to indicate the specific condition of each dungeon .
Each character is used to describe the specific situation of a dungeon unit .
among , Cells filled with rock obstacles are used ”#” Express , Empty cells without obstacles use ”.” Express , Your starting position is ”S” Express , End with ”E” Express .
Each character matrix will contain a blank line .
When entering a behavior ”0 0 0” when , Indicates that the input is terminated .
Output format
Each group of data outputs a result , Each result takes up one line .
If you can escape the dungeon , The output ”Escaped in x minute(s).”, among X The shortest time required to escape .
If you can't escape the dungeon , The output ”Trapped!”.
Data range
1≤L,R,C≤100
sample input :
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
sample output :
Escaped in 11 minute(s). Trapped!
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N=110;
char mp[N][N][N];// Store maps
int res[N][N][N];// Store the number of steps and mark
int l,r,c;
int xs,ys,zs;// The starting point
typedef struct {
int x,y,z;
}k;
int bfs()
{
// Three dimensional map , Six directions
int dx[6]={1,0,-1,0,0,0};
int dy[6]={0,1,0,-1,0,0};
int dz[6]={0,0,0,0,1,-1};
queue<k>q;
q.push({xs,ys,zs});// Read in start position
res[xs][ys][zs]=0;// The starting position distance is 0
while(!q.empty())
{
auto w=q.front();// Take out the first point in the queue
q.pop();
for(int i=0;i<6;i++)
{
int xx=w.x+dx[i];int yy=w.y+dy[i];int zz=w.z+dz[i];// Coordinates after offset
if(xx>=1&&xx<=l&&yy>=1&&yy<=r&&zz>=1&&zz<=c&&res[xx][yy][zz]==-1&&mp[xx][yy][zz]!='#')// Boundary judgment
{
res[xx][yy][zz]=res[w.x][w.y][w.z]+1;// Point distance after offset S Distance of
if(mp[xx][yy][zz]=='E')// eureka E spot , end
{
return res[xx][yy][zz];// Return steps
}
q.push({xx,yy,zz});// The offset point joins the team , Continue to search
}
}
}
return 0;// Can't find E, unsolvable
}
int main()
{
while(cin>>l>>r>>c&&l)
{
memset(res,-1,sizeof res);// Each time you enter a sample, you need to reset the map and steps
memset(mp,0,sizeof mp);
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int p=1;p<=c;p++)
{
cin>>mp[i][j][p];
if(mp[i][j][p]=='S')xs=i,ys=j,zs=p;// Record S The location of
}
}
}
int cnt=bfs();
if(cnt!=0)printf("Escaped in %d minute(s).\n",cnt);
else cout<<"Trapped!"<<endl;
}
return 0;
}
版权声明
本文为[Ice Cream~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534524684.html
边栏推荐
- Multi process model in egg -- egg document Porter
- How to realize adaptive layout
- Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行
- Deep learning object detection
- windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
- 踩坑:nacos利用startup.cmd -m standalone启动错误
- CMake基础教程(39)pkgconfig
- World and personal development
- 双击.jar包无法运行解决方法
- 提升独立站转化率攻略 | 挽回弃购用户
猜你喜欢
随机推荐
JS time format conversion
Similarities and differences between vector and array (notes)
Introduction to qqueue
Frequently asked interview questions - 1 (non technical)
Phlli in a VM node
Nécessité de précharger les cookies dans le sélénium
egg中的多进程模型--egg文档搬运工
分支与循环语句
The address value indicated by the pointer and the value of the object indicated by the pointer (learning notes)
uni使用的一些坑
d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
Solve the problem of JS calculation accuracy
Data mining -- understanding data
Self incrementing sequence creation of MySQL
QSslSocket::connectToHostEncrypted: TLS initialization failed
CORS and proxy (づ  ̄ 3  ̄) in egg ~ the process of stepping on the pit and filling the pit ~ tot~
Cmake basic tutorial (39) pkgconfig
STL learning notes 0x0001 (container classification)
Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator
Pytorch deep learning practice_ 11 convolutional neural network