D - Ice Cave(dfs)

你玩电脑游戏。你的角色站在一个多层次冰洞的某个层面上。为了向前走,你需要下降一层,唯一的方法就是从冰上掉下来。 你所在的洞穴的水平面是一个由N行和M列组成的矩形方格。每个细胞由完整的或破裂的冰组成。你可以从每个单元移动到与你相邻的单元(由于游戏引擎的某些限制,你不能在同一个地方跳跃,即从一个单元跳到它自己)。如果你移动到有碎冰的牢房,那么你的角色会从里面掉下来,如果你移动到有完整冰的牢房,那么这个牢房上的冰就会裂开。 让我们把整数从1到n的行从上到下编号,把整数从1到m的列从左到右编号。让我们把第r行和第c列的交集上的单元格表示为(r,c)。 你呆在牢房里(r1,c1),这个牢房破裂了,因为你刚从一个更高的地方掉下来。你需要通过单元格(R2,C2)倒下,因为到下一个层次的出口就在那里。你能做到吗?
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
Note
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.

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
char g[550][550];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int h,w,sx,sy,ex,ey;
int dfs(int x,int y)
{
 if(g[x][y]=='X')
 {
 	if(x==ex&&y==ey)
 	 return 1;
 	return 0;
 }
 g[x][y]='X'; 
 for(int i=0;i<4;i++)
 {
  int tx=x+dir[i][0],ty=y+dir[i][1];
  if(tx>=1&&tx<=h&&ty>=1&&ty<=w&&dfs(tx,ty))
   return 1;
 }	
 return 0;
}
int main()
{	
 ios::sync_with_stdio(false);
 cin>>h>>w;
 for(int i=1;i<=h;i++)
  for(int j=1;j<=w;j++)
  cin>>g[i][j];
 cin>>sx>>sy>>ex>>ey;
 g[sx][sy]='.';
 if(dfs(sx,sy))
  cout<<"YES"; 
 else
  cout<<"NO";
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值