codeforces ice cave

  1 ///
  2 ///    题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎
  3 ///    DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了;2. 若两点相邻,       
  4 ///    那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了;3. 普通的搜索看是否能到达,
  5 ///         若能还是要讨论终点踩几脚的问题:)
  6 ///    DFS 耗时大,险些超时,可以用BFS来做
  7 ///
  8 ///    自己写了一个能AC的,感觉太长比较挫,看到网上有比较短的,注释了下觉得应该自己以后应该写成这样
  9 #include <cstdio>
 10 #include <algorithm>
 11 #include <cstring>
 12 #include <queue>
 13 #include <string>
 14 #include <iostream>
 15 using namespace std;
 16 const int MAXN = 5e2 + 10;
 17 const int INF = 0x3f3f3f3f;
 18 int n, m;
 19 int sx, sy, ex, ey;
 20 char maze[MAXN][MAXN];
 21 bool vis[MAXN][MAXN];
 22 int dx[4] = {1, -1, 0, 0};
 23 int dy[4] = {0, 0, -1, 1};
 24 
 25 bool BFS(void)
 26 {
 27     queue<pair<int, int> > Q;///2个变量时使用pair,不写结构体,bfs能缩短几行代码
 28     Q.push (make_pair (sx, sy));
 29     while (!Q.empty ())
 30     {
 31         int x = Q.front ().first;
 32         int y = Q.front ().second;
 33         Q.pop ();
 34         for (int i=0; i<4; ++i)
 35         {
 36             int tx = x + dx[i];
 37             int ty = y + dy[i];
 38 
 39             if (tx == ex && ty == ey)    return true;
 40 
 41             if (tx <= n && tx >= 1 && ty <= m && ty >= 1 && maze[tx][ty] == '.')
 42             {
 43                 maze[tx][ty] = 'X';
 44                 Q.push (make_pair (tx, ty));
 45             }
 46         }
 47     }
 48 
 49     return false;
 50 }
 51 
 52 int main(void)
 53 {
 54     while (scanf ("%d%d", &n, &m) == 2)
 55     {
 56         memset (vis, 0, sizeof (vis));
 57         for (int i=1; i<=n; ++i)
 58         {
 59             scanf ("%s", maze[i]+1);
 60         }
 61         scanf ("%d%d", &sx, &sy);
 62         scanf ("%d%d", &ex, &ey);
 63 
 64         int cnt = 0;
 65         bool flag = false;
 66         ///终点四周'.'的个数
 67         for (int i=0; i<4; ++i)///未BFS前就处理出来,免得bfs后处理同样要多几行代码且麻烦,而且这里是处理了所有的情况
 68         {
 69             int tx = ex + dx[i];
 70             int ty = ey + dy[i];
 71             if (tx == sx && ty == sy)    flag = true;
 72             if (tx <= n && tx >= 1 && ty <= m && ty >= 1 && maze[tx][ty] == '.')    cnt++;
 73         }
 74 
 75         if (sx == ex && sy == ey)///为'X’,四周需要至少一个'.'来做铺垫(走到‘.’,再走回来)
 76         {
 77             if (cnt >= 1)    puts ("YES");
 78             else puts ("NO");
 79         }
 80         else if (flag)///起点和终点相邻,分终点是‘X’ 还是‘.’的情况
 81         {
 82             if (maze[ex][ey] == 'X')    puts ("YES");
 83             else
 84             {
 85                 if (cnt >= 1)    puts ("YES");///是‘.’的话 同样需要周围至少有一个'.'来做铺垫
 86                 else    puts ("NO");
 87             }
 88         }
 89         else
 90         {
 91            ///起点和终点不相邻的情况,BFS是否连通
 92             if (BFS () == true)
 93             {
 94                 if (maze[ex][ey] == 'X')    puts ("YES");
 95                 else if (cnt >= 2)    puts ("YES");///终点和起点不相邻时,终点周围至少要有2个.才能满足要求
 96                 else    puts ("NO");
 97             }
 98             else    puts ("NO");
 99         }
100     }
101     return 0;
102 }
原文地址:https://www.cnblogs.com/jiachinzhao/p/4546038.html