A计划(BFS)

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
 
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
原文地址:https://www.cnblogs.com/Fighting-sh/p/9886183.html