POJ-2251-Dungeon Master

來源:互聯網
上載者:User

因為考研一直在複習高數英語,結果連這麼簡單的題都不會,果然學習演算法還是需要不間斷的練習,今天把高數上冊學完了,就給自己放鬆一下練練演算法題。
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 43911 Accepted: 16567

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! 

Sample Input

3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

分析:寬度搜尋題,只是將原先的是二維,這道題是三維,方向變為6個了,
ps:貌似求迷宮的最短路徑一般都是用寬搜

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int MAXN = 35;const int INF = 0x3f3f3f3f;int dir[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};struct Index{    int x;    int y;    int z;};char map[MAXN][MAXN][MAXN];int dis[MAXN][MAXN][MAXN]; //儲存步數Index end;int bfs(Index start){    queue<Index> que;    memset(dis, INF, sizeof(dis)); //初始化所有位置 預設全部都是無法到達的(INF)    que.push(start); //將起點壓入隊列,並將初始步數設為0    dis[start.z][start.x][start.y] = 0;    while (que.size()) {        Index tempI = que.front(); que.pop(); //從隊列的最前端取出元素        if (tempI.z == end.z && tempI.x == end.x && tempI.y == end.y) { //如果取出的狀態已經是終點則結束搜尋            break;        }        Index loc; //移動後的位置記為loc        for (int i = 0; i < 6; i++) { //六個方向的迴圈            loc.z = tempI.z + dir[i][0];            loc.x = tempI.x + dir[i][1];            loc.y = tempI.y + dir[i][2];            //因為判斷條件太多所以用兩個if,判斷是否已經訪問過了(若未訪問dis[loc.z][loc.x][loc.y] == INF)            if (loc.x >= 0 && loc.x < r && loc.y >= 0 && loc.y < c && loc.z >= 0 && loc.z < l) {                if (map[loc.z][loc.x][loc.y] != '#' && dis[loc.z][loc.x][loc.y] == INF) {                    //若可以移動的話,則排入佇列,並且距離+1(因為你走了一步)                    que.push(loc);                    dis[loc.z][loc.x][loc.y] = dis[tempI.z][tempI.x][tempI.y] + 1;                }            }        }    }    return dis[end.z][end.x][end.y];}int main(){    int l, r, c;    while (scanf("%d%d%d", &l, &r, &c) != EOF) {        if (l==0 && r==0 && c==0) {            break;        }        int step_num; //起點到終點的步數如果step_num==INF則表示終點無法到達        Index start;        for (int i = 0; i < l; i++) {            for (int j = 0; j < r; j++) {                for (int k = 0; k < c; k++) {                    cin >> map[i][j][k];  //用c++輸入比用c輸入,可以很好的處理字串輸入問題                    if (map[i][j][k] == 'S') {                        start.x = j;                        start.y = k;                        start.z = i;                    }                    if (map[i][j][k] == 'E') {                        end.x = j;                        end.y = k;                        end.z = i;                    }                }            }        }//      printf("%d%d%d\n", end.z, end.x, end.y);        step_num = bfs(start);         if (step_num == INF) {             printf("Trapped!\n");        }        else {            printf("Escaped in %d minute(s).\n", step_num);        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.