Ice Cave CodeForces - 540C

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

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

写了好长时间,一开始想着用bfs,但是发现,我这个标记存在一点问题,然后又改成dfs,这样的话,我们的判断条件比较好判断。

还是讲一下,这个严谨性的问题,我们不能只是想到一些一般的情况,我们应该慢慢的想各种极限的情况,比如从0开始慢慢向上加,不要一口吃个胖子,这样的话,自己也会消化不良,自己的代码存在的问题,也就会笔记比较多。

#include<cstdio>
#include<queue>
#include<cmath>
#define bug(x)  printf("%d****\n",x);
using namespace std;
typedef long long ll;

char mp[510][510];
int vis[510][510];
int f_v[510][510];//防止超时,把已经走过4个方向的点进行标记,避免超时
int n,m;
int st_x,st_y,ed_x,ed_y;
int flag=0;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

int fd_fg=0;

void dfs(int nw_x,int nw_y){
	if(((nw_x==ed_x&&fabs(nw_y-ed_y)==1)||(nw_y==ed_y&&fabs(nw_x-ed_x)==1))){
	    fd_fg=1;
		return;
	}
	if(nw_x==ed_x&&nw_y==ed_y){
		fd_fg=1;
		return;
	}
	int nt_x,nt_y;
	for(int i=0;i<4;i++){
		nt_x=nw_x+dir[i][0];
		nt_y=nw_y+dir[i][1];
		if(f_v[nt_x][nt_y]||vis[nt_x][nt_y]||nt_x<1||nt_x>n||nt_y<1||nt_y>m||mp[nt_x][nt_y]=='X')
			continue;
		vis[nt_x][nt_y]=1;	
		dfs(nt_x,nt_y);
		if(fd_fg)
			return;
		vis[nt_x][nt_y]=0;
	}
	f_v[nw_x][nw_y]=1;
}

void check(){
	if(!fd_fg){
		printf("NO\n");
		return;
    }
	if(!flag&&fd_fg){
		printf("YES\n");
		return;
	}
	if(flag&&fd_fg){
	     for(int i=0;i<4;i++){
	     	int n_x,n_y;
	     	n_x=ed_x+dir[i][0];
	     	n_y=ed_y+dir[i][1];
	     	if(vis[n_x][n_y]||n_x<1||n_x>n||n_y<1||n_y>m||mp[n_x][n_y]=='X')
	     		continue;
	        printf("YES\n");
	        return ;
		 }
	}
	printf("NO\n");
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			scanf(" %c",&mp[i][j]);
		}
	}
	scanf("%d %d",&st_x,&st_y);
	scanf("%d %d",&ed_x,&ed_y);
	if(st_x==ed_x&&st_y==ed_y){
		 for(int i=0;i<4;i++){
	     	int n_x,n_y;
	     	n_x=ed_x+dir[i][0];
	     	n_y=ed_y+dir[i][1];
	     	if(n_x<1||n_x>n||n_y<1||n_y>m||mp[n_x][n_y]=='X')
	     		continue;
	        printf("YES\n");
	        return 0 ;
		 }
		 printf("NO\n"); 
		 return 0;
	} 
	if(mp[ed_x][ed_y]=='.')
		flag=1;
	dfs(st_x,st_y);
	check();
	return 0;
}


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值