Dungeon Master——BFS

【題目描述】

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! 

Sample Input

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

Sample Output

Escaped in 11 minute(s).
Trapped!

【題目分析】
一道很簡單的三維BFS,但是因為我剛開始的時候沒有在入隊的時候就設置該點已經入隊導致瘋狂入隊然后一直爆空間,還完全找不到問題
代碼

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;const int MAXN=35;
int a[MAXN][MAXN][MAXN];
char s[MAXN];
int L,R,C,u,v,w,x,y,z;
struct node
{int x,y,z;int step;
};
node S,E,p,t;const int drc[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int ans;void BFS()
{queue<node> q;q.push(S);while(!q.empty()){p=q.front(); q.pop();if(p.x==E.x && p.y==E.y && p.z==E.z){ans=p.step;while(!q.empty()) q.pop();return;}x=p.x; y=p.y; z=p.z;//a[z][x][y]=0;	//就因為這里一直錯for(int i=0;i<6;i++){u=x+drc[i][0]; v=y+drc[i][1]; w=z+drc[i][2];if(u<0 || u>=R || v<0 || v>=C || w<0 || w>=L) continue;if(a[w][u][v]==0) continue;t.x=u; t.y=v; t.z=w; t.step=p.step+1;a[w][u][v]=0;	//很重要q.push(t);}}
}int main()
{while(~scanf("%d%d%d",&L,&R,&C)){if(L==0 && R==0 && C==0) break;memset(a,0,sizeof(a));ans=0;for(int i=0;i<L;i++){for(int j=0;j<R;j++){scanf("%s",s);for(int k=0;k<C;k++){if(s[k]=='.') a[i][j][k]=1;else if(s[k]=='S'){S.z=i; S.x=j; S.y=k; S.step=0;a[i][j][k]=1;}else if(s[k]=='E'){E.z=i; E.x=j; E.y=k; E.step=0;a[i][j][k]=1;}}}}BFS();if(ans==0){printf("Trapped!\n");}else{printf("Escaped in %d minute(s).\n",ans);}}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384169.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384169.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384169.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux 環境 C語言 操作MySql 的接口范例

http://www.cnblogs.com/wunaozai/p/3876134.html 接上一小節&#xff0c;本來是計劃這一節用來講數據庫的增刪改查&#xff0c;但是在實現的過程中&#xff0c;出現了一點小問題&#xff0c;也不是技術的問題&#xff0c;就是在字符界面上比較不好操作。比如要注冊一個帳號&a…

二進制邏輯運算符有關練習題

//1.寫一個函數返回參數二進制中 1 的個數 #include<stdio.h> int div 0; //除數 int rem 0; //余數 int count 0; //計1 int count_one_bits(unsigned int div) {int con 0; //商while (div > 1){con div / 2;rem div % 2;div con;if (1 rem){count;}}…

Catch That Cow——BFS

【題目描述】 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo…

利用mysql提供的c語言接口操作數據庫

http://blog.csdn.net/bladeandmaster88/article/details/52980872 //1.工程要在c/c->常規->附加包含目錄添加mysql.h的路徑D:\mysql5.5\include //2.工程要在鏈接器->常規->附加庫目錄添加libmysql.lib的路徑D:\mysql5.5\lib #include <WinSock2.h>/…

數組相關運算

數組的初始化 數組及指針在內存中的存儲 一維數組在內存中的存儲 有關數組的運算 //一維數組 int a[] {1,2,3,4}; printf("%d\n",sizeof(a));//16這里的a表示的是整個數組,計算出的是整個數組的大小,單位為byte printf("%d\n",sizeof(a 0));/*a沒有單獨…

Find The Multiple——簡單搜索+大膽嘗試

【題目描述】 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more t…

C語言操作MYSQL小例子

http://blog.csdn.net/small_qch/article/details/8180678 初學使用用C語言操作MYSQL&#xff0c;寫了個小例子&#xff0c;帖上來獻丟人一下&#xff0c;呵呵。 程序很簡單&#xff0c;先連接數據庫&#xff0c;然后向class1表中插入一條數據&#xff0c;最后獲取并輸出整個cl…

Find a way——BFS

【題目描述】 Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in the ce…

用C語言操作MySQL數據庫

http://blog.chinaunix.net/uid-26743670-id-3479887.html 參考MYSQL的幫助文檔整理 這里歸納了C API可使用的函數&#xff0c;并在下一節詳細介紹了它們。請參見25.2.3節&#xff0c;“C API函數描述”。 函數 描述 mysql_affected_rows() 返回上次UPDATE、DELETE或INSERT查…

三字棋

整個游戲可以分為以下幾個環節 1.打印一個玩游戲的菜單 2.玩游戲 (1)玩家走一步 (2)電腦走一步 每走一步對結果進行顯示,其中游戲的結果可分為玩家贏,電腦贏,以及平局 代碼顯示如下 game.c #define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h> #include<stdio.h&…

gets fgets 區別

http://www.cnblogs.com/aexin/p/3908003.html 1. gets與fgets gets函數原型&#xff1a;char*gets(char*buffer);//讀取字符到數組&#xff1a;gets(str);str為數組名。 gets函數功能&#xff1a;從鍵盤上輸入字符&#xff0c;直至接受到換行符或EOF時停止&#xff0c;并將讀取…

Fliptile——搜索+二進制優化

【題目描述】 Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is col…

掃雷

1.將掃雷界面看成一個二維數組,先對界面進行打印 2.置雷 3.排雷 4.對每次的結果進行游戲輸出 5.提醒用戶游戲輸贏 game.c #define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" //初始化 void init_board(char mine[ROWS][COLS], int rows, int cols, char set) {in…

C相關練習題

1.調整數組使奇數全部都位于偶數前面。 輸入一個整數數組&#xff0c;實現一個函數&#xff0c;來調整該數組中數字的順序使得數組中所有的奇數位于數組的前半部分&#xff0c;所有偶數位于數組的后半部分。 #include<stdio.h> void range(int arr[], int sz) {int left…

【C語言】單鏈表的所有操作的實現(包括PopBack、PushBack、PopFront、PushFront、Insert)

http://blog.csdn.net/hanjing_1995/article/details/51539563 [cpp] view plaincopy #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; //單鏈表的實現 #include<assert.h> typedef int DataType; typedef…

Shuffle'm Up——簡單模擬

【題目描述】 A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several diff…

C++ explicit關鍵字詳解

http://www.cnblogs.com/ymy124/p/3632634.html 首先, C中的explicit關鍵字只能用于修飾只有一個參數的類構造函數, 它的作用是表明該構造函數是顯示的, 而非隱式的, 跟它相對應的另一個關鍵字是implicit, 意思是隱藏的,類構造函數默認情況下即聲明為implicit(隱式). 那么顯示聲…

Fire!——兩個BFS

【題目描述】 【題目分析】 看到題目后很清楚是兩個BFS&#xff0c;可是我覺得對于火的BFS可以轉換成判斷&#xff0c;我的做法是將火的位置全部記錄下來&#xff0c;然后判斷某個位置距離每個火的步數是否小于當前步數&#xff0c;可是錯了&#xff0c;還不清楚為什么&#x…

函數調用過程(棧楨)

棧楨 首先來看一段代碼 #include<stdio.h> int add(int x, int y) {int z x y;return z; } int main() {int a 10;int b 20;int ret add(a, b);printf("ret %d\n",ret);return 0; } 此處是為了給a,b分別開辟空間,這時棧楨如圖所示 兩條push命令將a,b變…

C++智能指針簡單剖析

http://blog.csdn.net/lanxuezaipiao/article/details/41603883 導讀 最近在補看《C Primer Plus》第六版&#xff0c;這的確是本好書&#xff0c;其中關于智能指針的章節解析的非常清晰&#xff0c;一解我以前的多處困惑。C面試過程中&#xff0c;很多面試官都喜歡問智能指針相…