【簡單搜尋】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
- POJ-2251 Dungeon MasterAST
- POJ2251 Dungeon MasterAST
- POJ 2251-Dungeon Master(BFS-三維迷宮)AST
- BFS廣度優先搜尋(3)--poj2251(zoj1940)(基礎題)
- Elasticsearch 實現簡單搜尋Elasticsearch
- ElasticSearch 簡單的 搜尋 聚合 分析Elasticsearch
- POJ 1691 Painting A Board(dfs搜尋)AI
- 自制簡單的詩歌搜尋系統
- 簡單檔案搜尋:Find Any File for MacMac
- SearchView的簡單使用和模擬搜尋View
- EasyFind for Mac操作簡單的檔案搜尋工具Mac
- laravel 簡單限制搜尋引擎爬蟲頻率Laravel爬蟲
- HDU 1241Oil Deposits(簡單搜尋題)
- Meteor+MongoDB 實現簡單的即時搜尋MongoDB
- POJ 3411 Paid Roads(搜尋的小技巧)AI
- 搜尋排序技術簡介排序
- DFS與BFS——理解簡單搜尋(中文虛擬碼+例題)
- 實戰:Nodejs+Mongodb+Elasticsearch 實現簡單的搜尋NodeJSMongoDBElasticsearch
- SimpleAISearch:C# + DuckDuckGo 實現簡單的AI搜尋AIC#Go
- POJ 1579-Function Run Fun(記憶化搜尋-遞迴)Function遞迴
- 使用 JavaScript 實現簡單候選項推薦功能(模糊搜尋)JavaScript
- BFS廣度優先搜尋(6)--poj3414(基礎題)
- 文字獲取和搜尋引擎簡介
- Linux 搜尋檔案和資料夾的 4 種簡單方法Linux
- 海量資料搜尋---搜尋引擎
- 【Leetcode 346/700】79. 單詞搜尋 【中等】【回溯深度搜尋JavaScript版】LeetCodeJavaScript
- MySQL單詞搜尋相關度排名MySql
- Django單元測試與搜尋引擎Django
- 快速整合搜尋介面下拉選單框架框架
- Android開源實戰:手把手教你實現一個簡單 & 好用的搜尋框(含歷史搜尋記錄)Android
- [每天get點新技能]搜商——搜尋發展簡史(1)
- [每天get點新技能]搜商——搜尋發展簡史(2)
- 搜尋引擎-03-搜尋引擎原理
- 《ElasticSearch6.x實戰教程》之簡單搜尋、Java客戶端(上)ElasticsearchJava客戶端
- vue+node全棧移動商城【5】-簡單的篩選搜尋功能Vue全棧
- iOS開發 iOS9 Spotlight 應用內搜尋簡單介紹iOS
- 簡單破解:電子郵件地址搜尋器------->高手莫入 (4千字)