HDU 2102 A计划

本题有几个坑注意一下就ok了。

1、碰到传送门一定传送;

2、地图两边都是传送门时不可行;

3、起点S不一定是(0,0,0)       //在这点wa了好几发 T_T

处理好这几点后就是一个裸的bfs。(也有大牛写的dfs,个人感觉bfs比较好)

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 
 6 struct node {
 7 int x,y,z;
 8 int step;
 9 void init (int nx,int ny,int nz,int nstep){
10 x=nx;y=ny;z=nz;step=nstep;
11 }
12 };
13 
14 int c,n,m,t;
15 int x,y,z;
16 char map[5][20][20];
17 int visit[5][20][20];
18 int dir[4][2]={0,1,0,-1,1,0,-1,0};
19 
20 int bfs (){
21 node a,b;
22 queue <node> q;
23 while (!q.empty ())
24 q.pop ();
25 a.init (x,y,z,0);
26 q.push (a);
27 while (!q.empty ()){
28 a=q.front ();
29 q.pop ();
30 if (a.x<0||a.x>=n||a.y<0||a.y>=m)
31 continue ;
32 if (map[a.z][a.x][a.y]=='#'){
33 a.z=(a.z+1)%2;
34 if (map[a.z][a.x][a.y]=='#')
35 continue ;
36 }
37 if (visit[a.z][a.x][a.y]||map[a.z][a.x][a.y]=='*'||a.step>t)
38 continue ;
39 visit[a.z][a.x][a.y]=1;
40 if (map[a.z][a.x][a.y]=='P')
41 return a.step<=t?1:0;
42 for (int i=0;i<4;i++){
43 b=a;
44 b.step++;
45 b.x+=dir[i][0];
46 b.y+=dir[i][1];
47 q.push (b);
48 }
49 }
50 return 0;
51 }
52 
53 int main (){
54 cin>>c;
55 while (c--){
56 memset (visit,0,sizeof visit);
57 
58 cin>>n>>m>>t;
59 for (int j=0;j<2;j++)
60 for (int i=0;i<n;i++){
61 cin>>map[j][i];
62 for (int o=0;o<m;o++)
63 if (map[j][i][o]=='S')
64 z=j,x=i,y=o;
65 }
66 
67 if (bfs ())
68 cout<<"YES"<<endl;
69 else
70 cout<<"NO"<<endl;
71 
72 }
73 return 0;
74 }
原文地址:https://www.cnblogs.com/gfc-g/p/3848113.html