• CodeForces


    题目链接:https://vjudge.net/contest/226823#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.

    The level of the cave where you are is a rectangular square grid of n rows and mcolumns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.

    Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).

    You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2)since the exit to the next level is there. Can you do this?

    Input

    The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.

    Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).

    The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.

    The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.

    Output

    If you can reach the destination, print 'YES', otherwise print 'NO'.

    Examples

    Input
    4 6
    X...XX
    ...XX.
    .X..X.
    ......
    1 6
    2 2
    Output
    YES
    Input
    5 4
    .X..
    ...X
    X.X.
    ....
    .XX.
    5 3
    1 1
    Output
    NO
    Input
    4 7
    ..X.XX.
    .XX..X.
    X...X..
    X......
    2 2
    1 6
    Output
    YES

    题意:

    在一个n*m的冰面上:当跳到‘.’上时,此位置变为‘X’,即碎裂;当跳到‘X’时,冰面穿了,人会掉到下一层去。问是否存在一条路径:使得人从起始点(必定为‘X’)跳到终点,然后恰好从终点掉到下一层。人可以上下左右跳。

    题解:

    1.当终点为‘X’时,直接bfs找到一条从起点到终点的路径即可。

    2.当终点为‘.’时,终点必须在同一条路径上出现两次。第一次时,终点变为‘X’,之后,为了跳回到终点,可选择跳到终点四侧可跳的位置,然后又调回到终点,即可满足要求。

    3.总和第1、2点,可直接bfs,当遇到终点为‘X’或者已经vis过时,即可满足条件。对于终点为‘X’,当然无可厚非,但对于终点为‘.’时,为什么vis过也可满足条件呢?要知道bfs是不确定路径上有谁的。答:对于能访问被vis过的终点,无非只有两种:1是路径上有终点,然后又跳了回来,这样当然可以;第二种是:路径上无终点,但既然它能跳到终点,就证明终点可以从这条路径上兜个圈再回来,所以同样满足条件。……太难表述啦

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <cstdlib>
     6 #include <string>
     7 #include <vector>
     8 #include <map>
     9 #include <set>
    10 #include <queue>
    11 #include <sstream>
    12 #include <algorithm>
    13 using namespace std;
    14 typedef long long LL;
    15 const double eps = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 9e18;
    18 const int MOD = 1e9+7;
    19 const int MAXN = 1e3+10;
    20 
    21 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
    22 char M[MAXN][MAXN];
    23 int n, m;
    24 
    25 struct node
    26 {
    27     int x, y;
    28 };
    29 
    30 queue<node> q;
    31 int vis[MAXN][MAXN];
    32 bool bfs(node st, node en)
    33 {
    34     memset(vis, 0, sizeof(vis));
    35     while(!q.empty()) q.pop();
    36 
    37     q.push(st);
    38     vis[st.x][st.y] = 1;
    39 
    40     node now, e;
    41     while(!q.empty())
    42     {
    43         now = q.front();
    44         q.pop();
    45 
    46         for(int i = 0; i<4; i++)
    47         {
    48             e.x = now.x + dir[i][0];
    49             e.y = now.y + dir[i][1];
    50             if(e.x<1||e.x>n||e.y<1||e.y>m) continue;
    51             if(e.x==en.x&&e.y==en.y&&(M[e.x][e.y]=='X'||vis[e.x][e.y]))
    52                 return true;
    53 
    54             if(vis[e.x][e.y]||M[e.x][e.y]=='X') continue;
    55             vis[e.x][e.y] = 1;
    56             q.push(e);
    57         }
    58     }
    59     return false;
    60 }
    61 
    62 int main()
    63 {
    64     while(cin>>n>>m)
    65     {
    66         for(int i = 1; i<=n; i++)
    67             scanf("%s",M[i]+1);
    68         node st, en;
    69         cin>>st.x>>st.y>>en.x>>en.y;
    70         if(bfs(st,en)) puts("YES");
    71         else puts("NO");
    72     }
    73 }
    View Code
  • 相关阅读:
    android UI 适配小节
    Android中的倒计时实现
    几种适配器&观察者&ListView之间的那点事
    Service stopSelf(int statId)和onStartcommand(Intent intent,int flags,int startId)
    java 虚函数
    java 重载和多态的区别
    小程序体验版路径以及参数携带
    微信小程序--上传图片公用组件
    微信小程序页面返回
    js兼容安卓和IOS的复制文本到剪切板
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8995354.html
Copyright © 2020-2023  润新知