CodeForces - 540C Ice Cave(BFS)

46 篇文章 0 订阅
Ice Cave

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

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.


题意:n*m的地图,"."代表完整的冰块,"X"代表碎裂的冰块,完整的冰块"."走一次后会变成碎裂的冰块"X",不能原地走,起点和终点可能在同一个位置
思路:记忆化BFS即可,"."走过后可以变成"X",若走到终点时终点为"X",则直接为YES。若终点为".",则将终点变为“X”继续走,判断是否能走到终点
ps:看这两组数据
       2 2            1 1
       . .               .
       . X             X
       2 2            1 1
       1 1            1 1
因为BFS是同时进行多条路线,所以当到达终点时第一组数据显示所有点都已走过,而实际上还有点没有走,只是走了其中一条路。。。。。wa在这上面好久 敲打

正确代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

#define maxn 500+10
struct node
{
    int x,y;
};
int to[][2]= {0,1,0,-1,1,0,-1,0};
char s[maxn][maxn];
int vis[maxn][maxn];
int n,m;

int bfs(int x1,int y1,int x2,int y2)
{
    memset(vis,0,sizeof(vis));
    node now,next;
    queue<node>q;
    now.x=x1,now.y=y1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=now.x+to[i][0],yy=now.y+to[i][1];
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy])
            {
                next.x=xx,next.y=yy;
                vis[xx][yy]=1;
                if(xx==x2&&yy==y2)
                {
                    if(s[x2][y2]=='X')
                        return 1;
                    else
                        s[x2][y2]='X',vis[xx][yy]=0;
                }
                else
                {
                    if(s[xx][yy]=='X')
                        continue;
                }
                q.push(next);
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%s",s[i]);
    int x1,y1,x2,y2,ans;
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    ans=bfs(x1-1,y1-1,x2-1,y2-1);
    if(ans)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


错误代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

#define maxn 500+10
struct node
{
    int x,y;
};
int to[][2]= {0,1,0,-1,1,0,-1,0};
char s[maxn][maxn];
int vis[maxn][maxn];
int n,m;

int check(int x,int y)
{
    if(s[x][y]=='X')
        return 1;
    for(int i=0; i<4; i++)
    {
        int xx=x+to[i][0],yy=y+to[i][1];
        if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&s[xx][yy]=='.')
            return 1;
    }
    return 0;
}

int bfs(int x1,int y1,int x2,int y2)
{
    memset(vis,0,sizeof(vis));
    node now,next;
    queue<node>q;
    now.x=x1,now.y=y1;
    vis[x1][y1]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
     //   printf("now.x=%d now.y=%d\n",now.x,now.y);
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=now.x+to[i][0],yy=now.y+to[i][1];
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy])
            {
                next.x=xx,next.y=yy;
                vis[xx][yy]=1;
                if(xx==x2&&yy==y2)
                    return check(x2,y2);
                else
                {
                    if(s[xx][yy]=='X')
                        continue;
                }

                q.push(next);
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%s",s[i]);
    int x1,y1,x2,y2,ans;
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    if(x1==x2&&y1==y2)
    {
        int flag=0;
        for(int i=0; i<4; i++)
        {
            int xx=x2-1+to[i][0],yy=y2-1+to[i][1];
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy]=='.')
            {
                flag=1;
                break;
            }
        }
        if(flag)
            ans=1;
        else
            ans=0;
    }
    else
        ans=bfs(x1-1,y1-1,x2-1,y2-1);
    if(ans)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值