1. 程式人生 > >廣度優先搜尋(BFS)----------------(TjuOj1140_Dungeon Master)

廣度優先搜尋(BFS)----------------(TjuOj1140_Dungeon Master)

這次整理了一下廣度優先搜尋的框架,以後可以拿來直接用了。TjuOj1140是一個三維的迷宮題,在BFS時我增加了一個控制陣列,用來對佇列的出隊進行控制,確保每次出隊的結點均為同一步長的結點,個人認為比較適合這種迷宮搜尋題。

BFS部分的程式碼如下:

int BFS ( node S , node T )
{
    int s = 1; //充當指標作用
    memset(n,0,sizeof(n));
    n[s] = 1;  //初始化當前距離為1的點數為1(即原點)
    node now = S;
    visit[now.z][now.y][now.x] = 1;
    node temp;
    queue
<node> Q; Q.push(now); while( !Q.empty() ){ for(int j = 0; j < n[s] ; j++ ){ //依次彈出所有距離為s的方塊,進行四周搜尋; now = Q.front(); Q.pop(); for(int i = 0 ; i < 6 ;i++){ //向6個方向探索是否有通路 temp.x = now.x + dx[i]; temp.y = now.y + dy[i]; temp.z
= now.z + dz[i]; if( visit[temp.z][temp.y][temp.x] == 0 && inmap(temp) ){ //防止越界訪問 temp.ch = maze[temp.z][temp.y][temp.x]; if (temp.ch == "E") return s; if (temp.ch == "."){ //如果探索到通路,將該通路標記為(當前距離+1) ,壓入佇列;
n[s + 1]++; //(當前距離+1)的方塊個數++ visit[temp.z][temp.y][temp.x] = 1; Q.push(temp); } } } } s++; } return 0; }

 

題目比較好理解,從S出發找E(但不一定有解),原文如下:

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 Specification

The input file consists of a number of dungeons. Each dungeon description starts with a line containing three integers  LR 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  LR and  C.

Output Specification

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!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

 

/*
 * 1140_Dungeon Master.cpp
 *
 *  Created on: 2018年11月14日
 *      Author: Jeason
 */

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <sstream>
#include <queue>
#define N 70
using namespace std;
int levels,rows,columns;
char maze[N][N][N];
int visit[N][N][N];
int n[ 100000 ] = {0};      //用於記錄每個距離的方塊個數;
string line;
int dx[6] = {1,-1,0,0,0,0};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {0,0,0,0,1,-1};

struct node {
    int x,y,z;
    string ch;
};

bool inmap(node A) {
    if(A.x < 0 || A.x >= columns) return false;
    if(A.y < 0 || A.y >= rows) return false;
    if(A.z < 0 || A.z >= levels) return false;
    return true;
}

int BFS(node S,node T)
{
    int s = 1; //充當指標作用
    memset(n,0,sizeof(n));
    n[s] = 1;  //初始化當前距離為1的點數為1(即原點)
    node now = S;
    visit[now.z][now.y][now.x] = 1;
    node temp;
    queue<node> Q;
    Q.push(now);
    while( !Q.empty() ){
        for(int j = 0; j < n[s] ; j++ ){          //依次彈出所有距離為s的方塊,進行四周搜尋;
            now = Q.front();
            Q.pop();
            for(int i = 0 ; i < 6 ;i++){   //向6個方向探索是否有通路
                temp.x = now.x + dx[i];
                temp.y = now.y + dy[i];
                temp.z = now.z + dz[i];
                if( visit[temp.z][temp.y][temp.x] == 0 && inmap(temp) ){   //防止越界訪問
                    temp.ch = maze[temp.z][temp.y][temp.x];
                    if (temp.ch == "E")
                        return s;
                    if (temp.ch == "."){      //如果探索到通路,將該通路標記為(當前距離+1) ,壓入佇列;
                        n[s + 1]++;                //(當前距離+1)的方塊個數++
                        visit[temp.z][temp.y][temp.x] = 1;
                        Q.push(temp);
                    }
                }
            }
        }
        s++;
    }
    return 0;
}


int main()
{
    node S,T;
    cin >> levels >> rows >> columns;

    while( (levels != 0)&&(rows != 0)&&(columns != 0) ){
        memset(maze, 0, sizeof(maze));          //讀入資料;
        memset(visit, 0, sizeof(visit));
        for(int i = 0;i < levels ;i++){
            for(int j = 0;j < rows ;j++){
                cin >> line;
                for(int k = 0;k < columns ;k++){
                    maze[i][j][k] = line[k];
                    if(line[k] == 'S'){         //找起點
                        S.z = i; S.x = k; S.y = j;
                        S.ch = "S";
                    }
                }
            }
        }
        int minutes = BFS( S, T );
        if(minutes != 0)
            cout << "Escaped in "<< minutes << " minute(s)."<< endl;
        else
            cout << "Trapped!" <<endl;
//        for(int i = 0;i < levels ;i++){      //輸出地圖
//            for(int j = 0;j < rows ;j++){
//                for(int k = 0;k < columns ;k++){
//                    cout << maze[i][j][k];
//                }
//                cout << endl;
//            }
//            cout << endl;
//        }
        cin >> levels >> rows >> columns;
    }

    return 0;
}

/*

Sample Input
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
*/