Ice Cave CodeForces

来源:互联网 发布:比利牛斯山的城堡 知乎 编辑:程序博客网 时间:2024/06/16 01:14

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 ther-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 andm (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 ofm characters "." (that is, intact ice) and "X" (cracked ice).

The next line contains two integers, r1 andc1 (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 andc2 (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 6X...XX...XX..X..X.......1 62 2
Output
YES
Input
5 4.X.....XX.X......XX.5 31 1
Output
NO
Input
4 7..X.XX..XX..X.X...X..X......2 21 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;}


原创粉丝点击