题目链接: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. 上传中文文件到linux文件出现乱码问题的解决方案

    convm -f gbk -t utf8 -r --notest /ftp的目录 效果:

  2. 内网ip打洞-----p2p实现原理

    网上找了非常多.代码大堆,原理讲清楚透彻的不多. 本人找几篇讲得好的来整理一下. 一片技术文章,最基本的讲清楚原理.假设再有完整的能执行的源码也可,关键是要把核心部分代码分析清楚. (1)问题的由来: ...

  3. C#面试:抽象类与接口

    本人近日面试遇到此等问题.然后又一次补习了一下下.希望对同行们有所帮助. 一.抽象类:       抽象类是特殊的类,仅仅是不能被实例化:除此以外.具有类的其它特性:重要的是抽象类能够包括抽象方法,这 ...

  4. HDU 4746 Mophues (莫比乌斯反演应用)

    Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others) Total ...

  5. 【Python】创建和使用类

    面向对象编程是最有效的软件编写方法之一 创建Dog类 class Dog(): '''一次模拟小狗的简单测试''' def __init__(self,name,age): self.name = n ...

  6. java中的值传递和引用传递区别

    值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...

  7. javascript判断智能终端信息

    < script type = "text/javascript" > /* * 智能机浏览器版本信息: * */ var browser = { versions: ...

  8. Windows下Tomcat+nginx配置证书实现登录页https访问

    最近公司出于安全考虑,需要将登录页做成https访问,其他页面仍采用http访问,环境是Linux平台,web服务器采用Tomcat + Nginx.之前没接触过nginx,这两天网上查资料,试了好多 ...

  9. Netty(五):Netty中如何序列化数据

    JDK提供了ObjectOutputStream和ObjectInputStream,用于通过网络对POJO的基本数据类型和图进行序列化和反序列化.该API并不复杂,而且可以被应用于任何实现了java ...

  10. php迭代器模式

    其实就是遍历数组 然后对数组中的元素进行操作 实现iterator接口即可.