【簡單搜尋】POJ 2251 Dugeon Master

Qky_gleaming發表於2020-10-20

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 line
Trapped!

簡單翻譯下題面:

一個有許多障礙物的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;
}

相關文章