【簡單搜尋】POJ 2251 Dugeon Master
Dungeon Master:
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 lineTrapped!
簡單翻譯下題面:
一個有許多障礙物的3D的迷宮,給你起點和終點,問你是否能從起點走到終點,如果能最快要幾分鐘(移動一步一分鐘);
分析:
顯然是一道搜尋的題,而對於這種求最短用時型別的題目一般都用廣搜。
因為廣搜的特性是一層層往外搜,遇到終止條件就結束搜尋,那麼這裡搜尋的層數就對應了題目裡的最短用時。
而深搜的特性是從一個點一直往下走,直到不能走就回退找另外的路。那麼在這道題裡即使找到了終點,你也無法確保你找到的路是用時最短的路,所以你需要列舉所有通往終點的路再進行比較才能得出答案。
這道題並沒有什麼難點,可以說是模板題。
三維(六個方向)的廣搜:
AC程式碼:
#include <cstdio>
#include <queue>
#include <string.h>
using namespace std;
int L,R,C;
char dungeon[35][35][35];
bool vis[35][35][35];
int direct[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
typedef struct{
int x,y,z;
int step;
}pos;
pos S; //起點
int bfs(pos start){
queue <pos> q;
q.push(start);
while(!q.empty()){
pos temp=q.front();
q.pop();
for(int i=0;i<6;i++){
pos new_pos;
new_pos.x=temp.x+direct[i][0];
new_pos.y=temp.y+direct[i][1];
new_pos.z=temp.z+direct[i][2];
new_pos.step=temp.step+1;
if(!vis[new_pos.x][new_pos.y][new_pos.z]&&new_pos.x<=L&&new_pos.x>=1&&new_pos.y<=R&&new_pos.y>=1&&new_pos.z<=C&&new_pos.z>=1&&dungeon[new_pos.x][new_pos.y][new_pos.z]!='#'){
vis[new_pos.x][new_pos.y][new_pos.z]=1;
if(dungeon[new_pos.x][new_pos.y][new_pos.z]=='E')
return new_pos.step;
q.push(new_pos);
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&L,&R,&C)){
if(L==0&&R==0&&C==0)
break;
memset(vis,0,sizeof(vis));
for(int i=1;i<=L;i++){
for(int j=1;j<=R;j++)
for(int k=1;k<=C;k++){
scanf(" %c",&dungeon[i][j][k]);
if(dungeon[i][j][k]=='S'){
S.x=i;
S.y=j;
S.z=k;
S.step=0;
}
}
}
int t=bfs(S);
if(t!=-1)
printf("Escaped in %d minute(s).\n",t);
else
printf("Trapped!\n");
}
return 0;
}
相關文章
- Dungeon Master(POJ-2251)AST
- POJ2251 Dungeon MasterAST
- POJ - 2236 Wireless Network (kuangbin - 簡單搜尋)
- POJ 1129 Channel Allocation (暴力搜尋)
- Elasticsearch 實現簡單搜尋Elasticsearch
- 百度簡單搜尋PC版玩法攻略 簡單搜尋有電腦版嗎?
- ElasticSearch 簡單的 搜尋 聚合 分析Elasticsearch
- Pots POJ – 3414 (搜尋+記錄路徑)
- 簡單檔案搜尋:Find Any File for MacMac
- 自制簡單的詩歌搜尋系統
- POJ1915,雙向寬度優先搜尋
- 單詞搜尋
- laravel 簡單限制搜尋引擎爬蟲頻率Laravel爬蟲
- EasyFind for Mac操作簡單的檔案搜尋工具Mac
- 簡單dp -- Common Subsequence POJ - 1458
- SimpleAISearch:C# + DuckDuckGo 實現簡單的AI搜尋AIC#Go
- 79. 單詞搜尋
- 單詞搜尋問題
- 搜尋排序技術簡介排序
- DFS與BFS——理解簡單搜尋(中文虛擬碼+例題)
- POJ 3259-Wormholes(簡單判負環)Worm
- LeetCode-079-單詞搜尋LeetCode
- Linux 搜尋檔案和資料夾的 4 種簡單方法Linux
- 【Leetcode 346/700】79. 單詞搜尋 【中等】【回溯深度搜尋JavaScript版】LeetCodeJavaScript
- POJ3087 Shuffle'm Up【簡單模擬】
- 文字獲取和搜尋引擎簡介
- 最佳路徑搜尋(二):啟發式搜尋(代價一致搜尋(Dijkstra search),貪心搜尋,A*搜尋)
- 海量資料搜尋---搜尋引擎
- Django單元測試與搜尋引擎Django
- [LeetCode題解]79. 單詞搜尋LeetCode
- MySQL單詞搜尋相關度排名MySql
- vue+node全棧移動商城【5】-簡單的篩選搜尋功能Vue全棧
- 《ElasticSearch6.x實戰教程》之簡單搜尋、Java客戶端(上)ElasticsearchJava客戶端
- Jina:在雲上構建神經網路搜尋的更簡單方法神經網路
- 搜尋
- Mysql Master-slave複製簡單配置記錄MySqlAST
- 搜尋引擎ElasticSearch18_ElasticSearch簡介1Elasticsearch
- 搜尋引擎-03-搜尋引擎原理