hdu

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

题目还是不难,注意起点一定是(0,0,0),然后到达P点时间<=t都可以。

用一个3维字符数组存储图,另一个三维数组标记走过的点。每次遇到#号传送过去之后,都要判断传过去的点是否被访问过。

并且还要注意传过去是‘*’的情况,和传来传去的情况都可以不用考虑。

没注意细节,WA了几次。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <iostream>
 5 using namespace std;
 6 char field[2][15][15];
 7 int used[2][15][15];
 8 int n,m,t;
 9 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
10 struct point
11 {
12     int z,x,y,time;
13 };
14 
15 int bfs()
16 {
17     memset(used,0,sizeof(used));
18     point s;
19     s.x=0,s.y=0,s.z=0,s.time=0;
20     queue<point>que;
21     que.push(s);
22     used[s.z][s.x][s.y]=1;
23     while(!que.empty())
24     {
25         point e=que.front();
26         que.pop();
27       //  printf("%d %d %d %d
",z,e.x,e.y,e.time);
28         if(field[e.z][e.x][e.y]=='P'&&e.time<=t) return 1;
29         for(int i=0;i<4;i++)
30         {
31             s=e;
32             s.x=e.x+dir[i][0];
33             s.y=e.y+dir[i][1];
34             if(!used[s.z][s.x][s.y]&&s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&field[s.z][s.x][s.y]!='*')
35             {
36                 if(field[s.z][s.x][s.y]=='#')
37                 {
38                     s.z^=1;
39                     if(used[s.z][s.x][s.y]) continue;
40                 }
41                 used[s.z][s.x][s.y]=1;
42                 s.time++;
43                 que.push(s);
44             }
45         }
46     }
47     return 0;
48 }
49 
50 int main()
51 {
52    //freopen("a.txt","r",stdin);
53     int c;
54     scanf("%d",&c);
55     while(c--)
56     {
57         scanf("%d%d%d",&n,&m,&t);
58         getchar();
59         for(int i=0;i<2;i++)
60         {
61             for(int j=0;j<n;j++)
62             {
63                 scanf("%s",field[i][j]);
64                // printf("%s
",field[i][j]);
65             }
66             //getchar();
67         }
68         for(int i=0;i<n;i++)
69             for(int j=0;j<m;j++)
70             {
71                 if(field[0][i][j]=='#'&&field[1][i][j]=='*') field[0][i][j]='*';
72                 if(field[1][i][j]=='#'&&field[0][i][j]=='*') field[1][i][j]='*';
73                 if(field[0][i][j]=='#'&&field[1][i][j]=='#') field[0][i][j]=field[1][i][j]='*';
74             }
75        // for(int i=0;i<2;i++)
76           //  for(int j=0;j<n;j++)
77            // printf("%s
",field[i][j]);
78         if(bfs()) printf("YES
");
79         else printf("NO
");
80     }
81     return 0;
82 }

因为并没要求求最快到达时间,并且n比较小,所以深搜也可以。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <iostream>
 5 using namespace std;
 6 char field[2][15][15];
 7 int used[2][15][15];
 8 int n,m,t,flag;
 9 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
10 
11 void dfs(int z,int x,int y,int time)
12 {
13     if(flag) return;
14   // printf("%d %d %d %d
",z,x,y,time);
15     if(field[z][x][y]=='P'&&time<=t)
16     {
17         flag=1;
18         printf("YES
");
19         return;
20     }
21     else if(field[z][x][y]=='#')
22     {
23         int zz;
24         if(z==0) zz=1;
25         else zz=0;
26         int xx=x;
27         int yy=y;
28         if(!used[zz][xx][yy]&&field[zz][xx][yy]!='*')
29         {
30             used[zz][xx][yy]=1;
31             dfs(zz,xx,yy,time);
32             used[zz][xx][yy]=0;
33         }
34     }
35     else
36     {
37         for(int i=0;i<4;i++)
38         {
39             int zz=z;
40             int xx=x+dir[i][0];
41             int yy=y+dir[i][1];
42             if(!used[zz][xx][yy]&&xx>=0&&xx<n&&yy>=0&&yy<m&&field[zz][xx][yy]!='*')
43             {
44                 if(time+1>t) continue;
45                 else
46                 {
47                     used[zz][xx][yy]=1;
48                     dfs(zz,xx,yy,time+1);
49                     used[zz][xx][yy]=0;
50                 }
51             }
52         }
53     }
54 }
55 
56 int main()
57 {
58   //freopen("a.txt","r",stdin);
59     int c;
60     scanf("%d",&c);
61     while(c--)
62     {
63         scanf("%d%d%d",&n,&m,&t);
64         getchar();
65         for(int i=0;i<2;i++)
66         {
67             for(int j=0;j<n;j++)
68             {
69                 scanf("%s",field[i][j]);
70                // printf("%s
",field[i][j]);
71             }
72             //getchar();
73         }
74         for(int i=0;i<n;i++)
75             for(int j=0;j<m;j++)
76             {
77                 if(field[0][i][j]=='#'&&field[1][i][j]=='*') field[0][i][j]='*';
78                 if(field[1][i][j]=='#'&&field[0][i][j]=='*') field[1][i][j]='*';
79                 if(field[0][i][j]=='#'&&field[1][i][j]=='#') field[0][i][j]=field[1][i][j]='*';
80             }
81        // for(int i=0;i<2;i++)
82           //  for(int j=0;j<n;j++)
83            // printf("%s
",field[i][j])
84         memset(used,0,sizeof(used));
85         flag=0;
86         used[0][0][0]=1;
87         dfs(0,0,0,0);
88         if(!flag) printf("NO
");
89     }
90     return 0;
91 }
原文地址:https://www.cnblogs.com/nowandforever/p/4527585.html