题目链接: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. Flak快速上手

    本文介绍如何上手 Flask . 这里假定你已经安装好了 Flask ,否则请先阅读< 安装>. 如果已安装好Flask,通过以下命令查看 一个简单的例子: from flask impo ...

  2. haifeng

    [root@localhost 桌面]# yum list|grep wubi ibus-table-chinese-wubi-haifeng.noarch -.el7 base ibus-table ...

  3. mysql下监测数据库语句creating sort index时间过长的问题

    在一张单表5000W数据上进行数据查询时传入两个单列索引条件,进行组合索引查询时,如果最后有order by id排序,与去除该排序,性能差距接近两个数量级 结论:在使用列的默认排序时,不应该再ord ...

  4. MongoDB入门学习(二):MongoDB的基本概念和数据类型

    上一篇讲了MongoDB的安装和管理,当中涉及到了一些概念,数据结构另一些API的调用,不知道的没关系,事实上非常easy,这篇会简介一下. 1.文档 文档是MongoDB的核心概念.多个键值对有序的 ...

  5. void 0 或者 undefined

    Problem 在检查一个值是否为undefined的时候.你们是假设去測试的? 要是之前的我会这样子測试 function isUndefined(obj){ return obj === unde ...

  6. 17:不重复整数提取NoRepeatNum

    题目描述 输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数. 输入描述:输入一个int型整数 输出描述:按照从右向左的阅读顺序,返回一个不含重复数字的新的整数 输入例子: ...

  7. C与C++在形參的一点小差别

    先看一下以下的代码: int fun(a,b) int a; int b; { return 10; } void main(int argc, char ** argv) { fun(10); re ...

  8. HTML5 2D平台游戏开发#2跳跃与二段跳

    在上一篇<Canvas制作时间与行为可控的sprite动画>中已经实现了角色的左右移动,本篇继续实现角色的一系列动作之一:跳跃.先来看看最终效果: 要实现跳跃,必须模拟垂直方向的速度和重力 ...

  9. 智能手机的耗电特征及APP耗电量测试的两种方法

    文章陈述了手机发展趋势及耗电特性,集中讨论了时下最为关心的智能手机耗电问题,并介绍了测量手机软件耗电量的两种方法.此外还解释了为何运营商此前会提出收取微信的费用,心跳机制是什么. 美国著名手机公司Pa ...

  10. HDU 2112 HDU Today(STL MAP + Djistra)

    题目链接:HDU Today 立即集训要開始,抓紧时间练练手,最短路的基础题,第一次用STL的map 题目非常水,可是错了N遍.手贱了.本题不优点理的就是把地名转化为数字 #include <i ...