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;
}
相關文章
- 1248:Dungeon MasterAST
- POJ2251 Dungeon MasterAST
- SGU 110 Dungeon(立體幾何)
- ! [rejected] master -> master (fetch first)AST
- git merge origin master git merge origin/master區別GitAST
- 文字冒險遊戲AI Dungeon 故事由AI寫就遊戲AI
- git rebase masterGitAST
- Scrum Master JobGPTScrumASTGPT
- MySQL5.7 Master-Master主主搭建for Centos7MySqlASTCentOS
- Master-Worker模式AST模式
- Master-Worker 模式AST模式
- 2 新增standby masterAST
- DocumentDB 報錯“not master”AST
- master..spt_valuesAST
- C. Game MasterGAMAST
- WPF master detail viewASTAIView
- Scrum Master 生存指南ScrumAST
- fatal: Not a valid object name: 'master'ObjectAST
- FSMO(Flexible Single Master Operation)FlexAST
- SAP Retail Merchandising Master DataAIAST
- (轉載)git pull origin master與git pull --rebase origin master的區別GitAST
- 在Git中,origin / master與origin master之間有什麼區別?GitAST
- 《AI Dungeon》開發者獲得投資,構建AI遊戲平臺AI遊戲
- MariaDB系列之三:基於日誌(binlog)主主複製(Master-Master)AST
- 使用kubeadm進行單master(single master)和高可用(HA)kubernetes叢集部署AST
- git關於origin和masterGitAST
- Master of Both —— Trie的應用AST
- WPF DataContext="{Binding SelectedItem,ElementName=_master}"ContextAST
- salt-master命令詳解AST
- 14-Architecture-Master-Node CommunicationAST
- 《Scrum Master進階白皮書》ScrumAST
- rocketMq叢集master模式搭建MQAST模式
- treevalue——Master Nested Data Like TensorAST
- Trino Master OOM 排查記錄ASTOOM
- Elasticsearch-04-master選舉Elasticsearch
- homebrew安裝問題(Failed during: git fetch origin master:refs/remotes/origin/master --tags --force)AIGitASTREM
- mysql MASTER_POS_WAIT函式MySqlASTAI函式
- Android uncovers master-key 漏洞分析AndroidAST