题目链接: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是路径上有终点,然后又跳了回来,这样当然可以;第二种是:路径上无终点,但既然它能跳到终点,就证明终点可以从这条路径上兜个圈再回来,所以同样满足条件。……太难表述啦

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int dir[][] = {,,,,-,,,-};
char M[MAXN][MAXN];
int n, m; struct node
{
int x, y;
}; queue<node> q;
int vis[MAXN][MAXN];
bool bfs(node st, node en)
{
memset(vis, , sizeof(vis));
while(!q.empty()) q.pop(); q.push(st);
vis[st.x][st.y] = ; node now, e;
while(!q.empty())
{
now = q.front();
q.pop(); for(int i = ; i<; i++)
{
e.x = now.x + dir[i][];
e.y = now.y + dir[i][];
if(e.x<||e.x>n||e.y<||e.y>m) continue;
if(e.x==en.x&&e.y==en.y&&(M[e.x][e.y]=='X'||vis[e.x][e.y]))
return true; if(vis[e.x][e.y]||M[e.x][e.y]=='X') continue;
vis[e.x][e.y] = ;
q.push(e);
}
}
return false;
} int main()
{
while(cin>>n>>m)
{
for(int i = ; i<=n; i++)
scanf("%s",M[i]+);
node st, en;
cin>>st.x>>st.y>>en.x>>en.y;
if(bfs(st,en)) puts("YES");
else puts("NO");
}
}

CodeForces - 540C Ice Cave —— BFS的更多相关文章

  1. CodeForces 540C Ice Cave (BFS)

    题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...

  2. CodeForces 540C Ice Cave (BFS)

    http://codeforces.com/problemset/problem/540/C       Ice Cave Time Limit:2000MS     Memory Limit:262 ...

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

    C. Ice Cave Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/problem/C ...

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

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

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

    http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on so ...

  6. BFS学习 Codeforces 301_div.2_Ice Cave

    C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  7. Codeforces 301_div.2_Ice Cave(BFS走冰块)

    Ice Cave Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Descripti ...

  8. CF#301 C:Ice Cave(简单BFS)

    C:Ice Cave 有一个m*n的地图,里面包含'.'表示完整的冰块,'X'表示有裂痕的冰块,当游戏者到达完整的冰块时,这个位置的冰块会变成有裂痕的冰块,如果到达有裂痕的冰块时,游戏者会进入下一关 ...

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

    题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...

随机推荐

  1. 怎样mac上安装apk到连接数据线的手机

    高大上的mac俺也用了一段时间了.不知道大家有木有同一个烦恼.曾经在win上的时候仅仅要安装了应用宝之类的手机助手.就能够双击APK,直接安装到连接数据线的手机上,非常方便哈,可是mac上不行.近期找 ...

  2. HDU 3657 Game(取数 最小割)经典

    Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. Shell脚本之:for

    与其他编程语言类似,Shell支持for循环. for循环一般格式为: for 变量 in 列表 do command1 command2 ... commandN done 列表是一组值(数字.字符 ...

  4. Node.js学习笔记(5)——关于child_process模块

    child_process是node一个比较重要的模块,通过它可以实现创建多线程,来利用多核CPU. 这个模块提供了四个创建子进程的函数. spawn.exec.execFile.fork. spaw ...

  5. 抽象类(abstract)和接口(interface)的区别

    1 抽象类是不能被实例化的类,只能作为由其他类继承的基类:    接口则定义了实现某种服务的一般规范(Objective-C中将接口称为“协议”(protocol)),声明了必需的函数和常量,但不指定 ...

  6. linux 内核(系统)、函数的理解、宏的程序调试

    1.操作系统 1.1.Linux 内核(系统)的组成的部分: 内核主要有:进程调度.内存管理.虚拟文件系统.网络接口和进程通信五个部分组成. (1)进程调度 进程调度是CPU对多个进程对CPU访问的调 ...

  7. swiper的理解

    参考:Swiper中文网 Swiper使用方法: <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  8. jquery实现重置

    $('#reset').click(function(){ $('#info_frm')[0].reset(); });

  9. Android-理解window和windowmanager

    1.window和windowmanager的关系 window是一个抽象类,具体实现为phoneWindow.创建一个window可以通过windowmanager来实现. window的具体实现在 ...

  10. Linux的经常使用命令(1) - 指定执行级别

    命令:init [0123456] 执行级别 0:关机 1:单用户 2:多用户状态没有网络服务 3:多用户状态有网络服务 4:系统未使用保留给用户 5:图形界面 6:系统重新启动 经常使用执行级别是3 ...