Dungeon Master(POJ-2251)
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!
分析
這道題用到了
三維
的陣列(在原先的二維陣列基礎上多一個引數記錄層數),地圖是類似於一層一層往上走的那種
由於是最短的時間的問題,所以我們採取bfs( )
進行搜尋
bfs()從6個方向搜
(因為是三維的,除了平面的走法還可以向上層走
),每次記錄時間miu[z][x][y]
,miu[z][x][y]是位於那個位置的時間,然後繼續往外擴一圈(6個方向都走一次算一圈),以此類推,直到碰觸到終點,時間即為最短
記得考慮一下邊界
問題
Code
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 1e2+22;
int level,row,col;
char map[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int miu[maxn][maxn][maxn];
int dx[6] = {1,0,0,-1,0,0};
int dy[6] = {0,1,0,0,-1,0};
int dz[6] = {0,0,1,0,0,-1};
struct node
{
int z,x,y;
node(int z,int x,int y)
{
this->z = z;
this->x = x;
this->y = y;
}
};
int bfs(node start , node end)
{
//int flag = 0;
//int cur = 0;
queue<node>q;
q.push(start);
// vis[start.x][start.y][start.z] = 1;
vis[start.z][start.x][start.y] = 1;
while(!q.empty())
{
node p =q.front();
q.pop();
int x = p.x;
int y = p.y;
int z = p.z;
for(int i = 0 ; i < 6 ; i++)
{
int next_z = z + dz[i];
int next_x = x + dx[i];
int next_y = y + dy[i];
node tp = node(next_z,next_x,next_y);
if( tp.z == end.z && tp.x == end.x && tp.y == end.y )
{
return miu[z][x][y]+1;
}
if(vis[tp.z][tp.x][tp.y] == 0 && map[tp.z][tp.x][tp.y] == '.' && tp.z >= 0 && tp.z < level && tp.x >= 0 && tp.x < row && tp.y >= 0 && tp.y < col)
{
miu[tp.z][tp.x][tp.y] = miu[z][x][y] + 1;
//cur = miu[tp.x][tp.y][tp.z];
//printf("miu[%d][%d][%d] = %d\n",tp.x,tp.y,tp.z,miu[tp.x][tp.y][tp.z]);
vis[tp.z][tp.x][tp.y] = 1;
q.push(tp);
}
}
}
return -maxn;
}
int main()
{
while(scanf("%d %d %d",&level,&row,&col))
{
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
memset(miu,0,sizeof(miu));
node start = node(0,0,0);
node endin = node(0,0,0);
//miu = 0;
if(level == 0 && row == 0 && col == 0){ break;}
for(int k = 0 ; k < level ; k++)
{
for(int i = 0 ; i < row ; i++)
{
for(int j = 0 ; j < col ; j++)
{
cin>>map[k][i][j];//第k層的第i行j列
}
}
}
for(int k = 0 ; k < level ; k++)
{
for(int i = 0 ; i < row ; i++)
{
for(int j = 0 ; j < col ; j++)
{
if(map[k][i][j] == 'S'){ start = node(k,i,j);}
if(map[k][i][j] == 'E'){ endin = node(k,i,j);}
}
}
}
int ans = bfs(start,endin);
if(ans == -maxn)
{
cout<<"Trapped!"<<endl;
}else
{
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
}
return 0;
}
相關文章
- POJ-2251 Dungeon MasterAST
- 1248:Dungeon MasterAST
- POJ2251 Dungeon MasterAST
- POJ 2251-Dungeon Master(BFS-三維迷宮)AST
- 視訊遊戲之 Shattered Pixel Dungeon遊戲
- MySQL Master/Slave Master/MasterMySqlAST
- SGU 110 Dungeon(立體幾何)
- ! [rejected] master -> master (fetch first)AST
- Mysql5.6 Master+MasterMySqlAST
- 文字冒險遊戲AI Dungeon 故事由AI寫就遊戲AI
- Mongodb的master-slave模式與master-master模式實驗MongoDBAST模式
- 《AI Dungeon》開發者獲得投資,構建AI遊戲平臺AI遊戲
- change master 未指定master_log_fileAST
- Git branching: master vs. origin/master vs. remotes/origin/masterGitASTREM
- show master logs 和 show master status 區別AST
- 【HBase】start master 與 start master --backup 的區別AST
- git rebase masterGitAST
- Spark master standalongSparkAST
- Redis master and slaveRedisAST
- MySQL master/slaveMySqlAST
- Scrum Master JobGPTScrumASTGPT
- MySQL5.7 Master-Master主主搭建for Centos7MySqlASTCentOS
- 2 新增standby masterAST
- Master-Worker 模式AST模式
- Scrum Master 生存指南ScrumAST
- Master MVVM in Swift<1>ASTMVVMSwift
- Master Note for Tablespace IssuesAST
- Master-Worker模式AST模式
- 母版頁(Master Pages)AST
- MySql的Master/SlaveMySqlAST
- Master Data Management UpdateAST
- KSV master waitASTAI
- WPF master detail viewASTAIView
- DocumentDB 報錯“not master”AST
- C. Game MasterGAMAST
- master..spt_valuesAST
- git merge origin master git merge origin/master區別GitAST
- 對 Git 分支 master 和 origin/master 的一些認識GitAST