Ice Cave

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

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 you are is a rectangular square grid of n rows and m columns. 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'.

Sample Input

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

Hint(看下面)

In the first sample test one possible path is:

After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.

BFS搜索!!很明显了!

要注意的是冰块踩过会变成X,下一次踩就会掉下去。

题解:pair是一种模板类型,其中包含两个数据值,两个数据的类型可以相同可以不同 ,由于pair类型的使用比较繁琐,因为如果要定义多个形同的pair类型的时候,可以时候typedef简化声明:
typedef pair<int, int>p;p中含有2个整型数

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> p;
int dx[]= { , -, , };
int dy[]= { , , , - };
int n,m;
int r1,c1,r2,c2;
char a[][];
int bfs()
{
queue<p> q;
q.push(p(r1,c1));
a[r1][c1]='X'; //第一步上去冰块已碎
while(!q.empty())
{
r1=q.front().first;
c1=q.front().second;
q.pop();
for(int i=; i<; i++)
{
int xx=r1+dx[i];
int yy=c1+dy[i];
if( xx<||xx>=n||yy<||yy>=m)
continue;
if(a[xx][yy]=='X')
{
//到达点也会碎
if(xx==r2 && yy==c2)
return ;
continue;
}
a[xx][yy]='X'; //走了之后就变成易碎的冰块
q.push(p(xx,yy));
}
}
return ;
}
int main()
{
int ans;
while(cin>>n>>m)
{
for(int i=; i<n; i++)
cin>>a[i];
cin>>r1>>c1>>r2>>c2;
r1--; //用的二维数组是从0~n-1,而输入的是1~n,所以都要减1
c1--;
r2--;
c2--;
ans=bfs();
if(ans)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}

Codeforces 301_div.2_Ice Cave(BFS走冰块)的更多相关文章

  1. 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 ...

  2. CodeForces 540C Ice Cave (BFS)

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

  3. CodeForces - 540C Ice Cave —— BFS

    题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...

  4. CodeForces 540C Ice Cave (BFS)

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

  5. 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 ...

  6. Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)

    D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. Fair CodeForces - 987D(巧妙bfs)

    题意: 有n个城市 m条边,每条边的权值为1,每个城市生产一种商品(可以相同,一共k种),求出分别从每个城市出发获得s种商品时所走过路的最小权值 解析: 我们倒过来想,不用城市找商品,而是商品找城市, ...

  8. codeforces 1072D Minimum path bfs+剪枝 好题

    题目传送门 题目大意: 给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径. 题解: (先吐槽一句,cf 标签是dfs题????) ...

  9. Knight Moves(BFS,走’日‘字)

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

随机推荐

  1. 在VC中集成cURL

    libcurl 库的代码是完全开源的,但是我们一般不会在项目中直接引入它的源代码,而是通过动态链接库隐式链接的方式引入 libcrul 库.也就是说,我们需要在自己的项目中引入 libcrul 相关的 ...

  2. stl中的空间配置器

    一般我们习惯的c++内存配置如下 class Foo { ... }; Foo* pf = new Foo; delete pf; 这里的new实际上分为两部分执行.首先是先用::operator n ...

  3. python关键字、转义符和字符串格式化

    最近在学learn python the hard way,学习到第37章,进行了关于关键字.转义符和字符串格式化的总结.看手头上的中文版没有及时更新.于是就把这些翻译过来,以作查阅. 关键字: 关键 ...

  4. maven src/test/resources 下的logback-test.xml 读取 properties文件中的key-value值

    <profiles>        <profile>            <id>test-cd</id>            <prope ...

  5. 【最长上升子序列】HDU 1087——Super Jumping! Jumping! Jumping!

    来源:点击打开链接 最长上升子序列的考察,是一个简单的DP问题.我们每一次求出从第一个数到当前这个数的最长上升子序列,直至遍历到最后一个数字为止,然后再取dp数组里最大的那个即为整个序列的最长上升子序 ...

  6. MVC4中EasyUI Tree异步加载JSON数据生成树

      1,首先构造tree接受的格式化数据结构MODEL /// <summary> /// 定义EasyUI树的相关数据,方便控制器生成Json数据进行传递 /// </summar ...

  7. Excel导入mysql数据库

    步骤一:选取要导入的数据快儿,另外要多出一列,如下图:    步骤二:  将选中的数据快儿拷贝到一个新建的表格工作薄,然后“另存为” ->“文本文件(制表符分割)(*.txt)”,假如存到“D: ...

  8. JSP执行过程详解

    复习JSP的概念 JSP是Java Server Page的缩写,在传统的HTML页面中加入JSP标签和java的程序片段就构成了JSP. JSP的基本语法:两种注释类型.三个脚本元素.三个元素指令. ...

  9. js~this的陷阱

    在JS中,当前对象一般用this表示,在jquery中,当前的对象是用$(this)表示,这些都是最基础的知识,没什么可说的,但我要说的是,当this出现在某个深度时,它的含义你自己要清楚,它是指离当 ...

  10. IntentFilter

    当Intent在组件间传递时,组件如果想告知Android系统自己能够响应和处理哪些Intent,那么就需要用到IntentFilter对象. 顾名思义,IntentFilter对象负责过滤掉组件无法 ...