Ice Cave(bfs专题)

原题面

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?

输入:

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

输出:
If you can reach the destination, print ‘YES’, otherwise print ‘NO’.

翻译结果(有误,可结合原题面分析):

你玩电脑游戏。你的角色站在一个多层冰洞的某个层面上。为了继续前进,你需要下降一层,而唯一的方法就是穿过冰面。

你所在洞穴的水平是一个由n行m列组成的矩形正方形网格。每个细胞要么来自完整的,要么来自破碎的冰。你可以从每个单元格移动到与你相邻的单元格(由于游戏引擎的一些限制,你不能在同一个地方跳跃,即从一个单元格跳到它自己)。如果你移动到有碎冰的牢房,那么你的角色就会掉进去,如果你移动到有完整冰的牢房,那么这个牢房上的冰就会破裂。

让我们从上到下对整数从1到n的行进行编号,从左到右对整数从1到m的列进行编号。我们把第r行和第c列的交点上的一个单元格表示为(r, c)

你呆在细胞里(r1, c1)这个细胞破裂了因为你从更高的地方掉下来了。你需要穿过细胞(r2, c2),因为下一层的出口在那里。你能做到吗?

输入

第一行包含两个整数,n和m(1≤n, m≤500)——洞穴描述中的行数和列数。

接下来的n行描述了洞穴的初始状态,每一行由m个字符组成。(即完整的冰)和“X”(碎冰)。

下一行包含两个整数,r1和c1(1≤r1≤n, 1≤c1≤m)——初始坐标。可以保证洞穴的描述中包含cell (r1, c1)中的字符“X”,即开始时cell上的冰最初是破裂的。

下一行包含两个整数r2和c2(1≤r2≤n, 1≤c2≤m) -您需要下落的单元格坐标。最后一个单元可能与开始的单元重合。

输出

如果您可以到达目的地,请打印“YES”,否则打印“NO”。

解题思路

该题需要注意的地方在于不是到达终点即可,还需要保证到达时终点不是’.’,而是’X’。
使用BFS,如果终点在当前位置的上、下、左或右并且终点状态为’X’即可。

代码

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
int n,m,sx,sy,ex,ey,flag=0;
struct node
{
    int x,y;
} a,b;
int mov[4][2]= {1,0,0,-1,0,1,-1,0};
queue<node> ppp;
char mp[505][505];
int bj[505][505];
int main ()
{
    scanf("%d %d",&n,&m);
    getchar();
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            scanf("%c",&mp[i][j]);
        }
        getchar();
    }
    scanf("%d %d %d %d",&sx,&sy,&ex,&ey);
    a.x=sx-1;
    a.y=sy-1;
    ex--;
    ey--;
    ppp.push(a);
    while(!ppp.empty())
    {
        a=ppp.front()
          ppp.pop();
          //核心判断
        if((mp[a.x-1][a.y]=='X'&&a.x-1==ex&&a.y==ey)||(mp[a.x][a.y-1]=='X'&&a.x==ex&&a.y-1==ey)||(mp[a.x+1][a.y]=='X'&&a.x+1==ex&&a.y==ey)||(mp[a.x][a.y+1]=='X'&&a.x==ex&&a.y+1==ey))
        {
            flag=1;
            break;
        }
        for(int i=0; i<4; i++)
        {
            b.x=a.x+mov[i][0];
            b.y=a.y+mov[i][1];
            if(b.x<0||b.y<0||b.x>=n||b.y>=m||bj[b.x][b.y]||mp[b.x][b.y]=='X')
                continue ;
            ppp.push(b);
            mp[b.x][b.y]='X';
            bj[b.x][b.y]=1;
        }
    }
    if(flag==1)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值