1. 程式人生 > >A計劃(BFS)

A計劃(BFS)

進入 mission bfs pop dir tin size ont names

A計劃

http://acm.hdu.edu.cn/showproblem.php?pid=2102

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30734 Accepted Submission(s): 7694


Problem Description 可憐的公主在一次次被魔王擄走一次次被騎士們救回來之後,而今,不幸的她再一次面臨生命的考驗。魔王已經發出消息說將在T時刻吃掉公主,因為他聽信謠言說吃公主的肉也能長生不老。年邁的國王正是心急如焚,告招天下勇士來拯救公主。不過公主早已習以為常,她深信智勇的騎士LJ肯定能將她救出。
現據密探所報,公主被關在一個兩層的迷宮裏,迷宮的入口是S(0,0,0),公主的位置用P表示,時空傳輸機用#表示,墻用*表示,平地用.表示。騎士們一進入時空傳輸機就會被轉到另一層的相對位置,但如果被轉到的位置是墻的話,那騎士們就會被撞死。騎士們在一層中只能前後左右移動,每移動一格花1時刻。層間的移動只能通過時空傳輸機,且不需要任何時間。

Input 輸入的第一行C表示共有C個測試數據,每個測試數據的前一行有三個整數N,M,T。 N,M迷宮的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宮的第一層的布置情況,後N*M表示迷宮第二層的布置情況。

Output 如果騎士們能夠在T時刻能找到公主就輸出“YES”,否則輸出“NO”。

Sample Input 1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..

Sample Output YES

Source HDU 2007-6 Programming Contest

Recommend xhd

終點可以在0,0,0...

技術分享圖片
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<string>
 5 #include<queue>
 6 #include<cstdio>
 7 using namespace std;
 8 int n,m,t;
 9 struct sair{
10     int x,y,z,step;
11 };
12 
13 char map[5][15][15];
14
int book[5][15][15]; 15 int dir[4][2]={0,1,1,0,0,-1,-1,0}; 16 17 bool bfs(){ 18 sair s,e; 19 s.x=0,s.y=0,s.z=0,s.step=0; 20 queue<sair>Q; 21 Q.push(s); 22 book[s.z][s.x][s.y]=1; 23 while(!Q.empty()){ 24 s=Q.front(); 25 Q.pop(); 26 if(map[s.z][s.x][s.y]==P){ 27 if(t>=s.step){ 28 cout<<"YES"<<endl; 29 return true; 30 } 31 return false; 32 } 33 for(int i=0;i<4;i++){ 34 e.z=s.z; 35 e.x=s.x+dir[i][0]; 36 e.y=s.y+dir[i][1]; 37 if(e.x>=0&&e.x<n&&e.y>=0&&e.y<m&&map[e.z][e.x][e.y]!=*&&!book[e.z][e.x][e.y]){ 38 book[e.z][e.x][e.y]=1; 39 e.step=s.step+1; 40 if(map[e.z][e.x][e.y]==#){ 41 e.z^=1; 42 if(map[e.z][e.x][e.y]==*||map[e.z][e.x][e.y]==#) continue; 43 } 44 Q.push(e); 45 } 46 } 47 } 48 return false; 49 } 50 51 int main(){ 52 int T; 53 cin>>T; 54 while(T--){ 55 cin>>n>>m>>t; 56 memset(book,0,sizeof(book)); 57 for(int i=0;i<n;i++){ 58 cin>>map[0][i]; 59 } 60 getchar(); 61 for(int i=0;i<n;i++){ 62 cin>>map[1][i]; 63 } 64 if(!bfs()) cout<<"NO"<<endl; 65 } 66 }
View Code

A計劃(BFS)