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 }
时间: 2024-10-07 16:05:49

codeforces ice cave的相关文章

DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

题目传送门 1 /* 2 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 3 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两点相邻, 4 那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了:3. 普通的搜索看是否能到达, 5 若能还是要讨论终点踩几脚的问题:) 6 DFS 耗时大,险些超时,可以用BFS来做 7 */ 8 #include <cstdio> 9 #include <algorit

CodeForces 540C Ice Cave (BFS)

http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 540C Description You play a computer game. Your character stands on some level of

(简单广搜) Ice Cave -- codeforces -- 540C

http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the

CF #301 504C C. Ice Cave BFS

C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to

2020腾讯笔试--Ice Cave

链接:http://codeforces.com/contest/540/problem/C You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the

ICE CAVE(BFS搜索(模拟))

Description You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where y

ice cave

Description You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where y

C. Ice Cave (CF #301 (Div. 2) 搜索bfs)

C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to

「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)

题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是这题...是冰塔的一层 也就是说,它只是个稍微有点限制的二维迷宫问题. 后面就好理解了,不过需要考虑下这种数据: 1 2 XX 1 1 1 1 这种数据答案是no.解决的方法可以考虑这样:分成两个数组来记录访问状态:vis数组和block数组. 代码 #include <queue> #inclu