HDU2102 A计划

解题思路:一道简单题,却WA了十几发,犯几个低级错误。还是不能急躁,

        内心要平静,具体分析见代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 const int maxn = 15;
 8 char mapp[maxn][maxn][2];
 9 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
10 int n, m, t, c;
11 
12 struct node{
13     int x, y;
14     int f;
15     int step;
16 }s, e;
17 
18 queue<node> q;
19 
20 int bfs()
21 {
22     while(!q.empty()) q.pop(); //没加这步WA了好多发
23     q.push(s);
24 
25     while(!q.empty())
26     {
27         s = q.front(); q.pop();
28         //debug时可打印路径
29         //printf("s.x = %d, s.y = %d, s.step = %d, s.flag = %d\n", s.x, s.y, s.step, s.flag);
30 
31 
32         for(int i = 0; i < 4; i++)
33         {
34             e.x = s.x + dir[i][0];
35             e.y = s.y + dir[i][1];
36             e.step = s.step + 1;
37             e.f = s.f;
38 
39             if(e.step > t || mapp[e.x][e.y][e.f] == '*') continue;//超过时间或不能走
40             if(mapp[e.x][e.y][e.f] == '#')
41             {
42                 mapp[e.x][e.y][e.f] = '*'; //标记为已走过
43                 e.f = 1 - e.f; //穿越到另一层
44                 //另一层若为#或*则标记为不能走
45                 if(mapp[e.x][e.y][e.f] == '#' || mapp[e.x][e.y][e.f] == '*')
46                 {
47                     mapp[e.x][e.y][e.f] = '*';
48                     continue; //必不可少
49                 }
50             }
51 
52             if(mapp[e.x][e.y][e.f] == 'P') return 1; //找到公主
53             mapp[e.x][e.y][e.f] = '*'; //标记为已走过
54             q.push(e);
55         }
56     }
57     return 0; //规定时间没找到
58 }
59 
60 int main()
61 {
62     scanf("%d", &c);
63     while(c --)
64     {
65         memset(mapp, '*', sizeof(mapp));
66         scanf("%d %d %d", &n, &m, &t);
67 
68         for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++)
69         scanf(" %c", &mapp[i][j][0]);
70 
71         for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++)
72         scanf(" %c", &mapp[i][j][1]);
73 
74         s.x = 1, s.y = 1, s.step = 0, s.f = 0;
75         if(bfs()) printf("YES\n");
76         else printf("NO\n");
77     }
78     return 0;
79 }
View Code

 

posted on 2015-10-17 12:16  改写历史,倾尽天下  阅读(140)  评论(0编辑  收藏  举报

导航