(简单广搜) Ice Cave -- codeforces -- 540C

http://codeforces.com/problemset/problem/540/C

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

Sample test(s)

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.

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;

#define N 550

char G[N][N];
int  a[N][N], n, m;
int dir[4][2] = {{-1,0},{0,-1},{0,1},{1,0}};

struct node
{
    int x, y;
}Start, End;

int BFS()
{
    int i;
    node p, q;

    queue<node>Q;
    Q.push(Start);

    ///a[Start.x][Start.y]--; 由于题意理解错误, 死在了这里

    while(Q.size())
    {
        p = Q.front(), Q.pop();

        for(i=0; i<4; i++)
        {
            q.x = p.x + dir[i][0];
            q.y = p.y + dir[i][1];

            if(q.x==End.x && q.y==End.y && a[q.x][q.y]==1) return 1;

            if(q.x>=0 && q.x<n && q.y>=0 && q.y<m && a[q.x][q.y]==2)
            {
                a[q.x][q.y]--;
                Q.push(q);
            }
        }
    }

    return 0;
}

int main()
{
    int i, j;
    scanf("%d%d", &n, &m);

    memset(G, 0, sizeof(G));
    memset(a, 0, sizeof(a));

    for(i=0; i<n; i++)
    {
        scanf("%s", G[i]);
        for(j=0; j<m; j++)
        {
            if(G[i][j] == ‘X‘)
                a[i][j] = 1;
            if(G[i][j] == ‘.‘)
                a[i][j] = 2;
        }
    }

    scanf("%d%d", &Start.x, &Start.y);
    scanf("%d%d", &End.x, &End.y);

    Start.x--, Start.y--;
    End.x--, End.y--;

    int ans = BFS();

    if(ans)
        printf("YES\n");
    else
        printf("NO\n");

    return 0;
}
时间: 2024-07-29 14:12:11

(简单广搜) Ice Cave -- codeforces -- 540C的相关文章

poj 3126 prime path 简单广搜

http://poj.org/problem?id=3126 题意:给你两个四位数a,b,从a开始    每次只能改变上一次数的其中一位,问至少需要几步才能得到b 分析:求最小路   典型的广搜   表面上是    40入口的bfs   但是除去有的数不是素数   入口数远小于40 可以写一个  判断一个数是否为素数的函数  , 每次去 调用 判断一个数是否要进队列 也可以 事先打一个素数表  这样会快点 注意:output  :either with a number stating the

Prime Path POJ - 3126(简单广搜)

题目链接:https://cn.vjudge.net/problem/POJ-3126 注意:预处理1000-9999间的素数,并且对入过队列的数进行标记,防止重复入队,否则超时 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 #include <algorithm> 6 #include <cmath> 7 #de

poj 3278:Catch That Cow(简单一维广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

CodeForces 540C Ice Cave (BFS)

http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 540C Description You play a computer game. Your character stands on some level of

HDU 2267 How Many People Can Survive(广搜,简单)

题目 //一道简单的广搜水题 #include<queue> #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; struct tt { int x,y; }; char mp[310][310]; int vis[310][310]; //看了题解,发现只有4个方向,而不是8个方向....题目貌似都没说清楚

HDU 1253 (简单三维广搜) 胜利大逃亡

奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #include <cmath> 7 using namespace std; 8 9 struct Poin

HDU 1240 (简单三维广搜) Asteroids!

给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数 三维的问题无非就是变成了6个搜索方向 最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y] 总之,这是很基础的一道题 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #include <algorithm

DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

题目传送门 1 /* 2 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 3 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两点相邻, 4 那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了:3. 普通的搜索看是否能到达, 5 若能还是要讨论终点踩几脚的问题:) 6 DFS 耗时大,险些超时,可以用BFS来做 7 */ 8 #include <cstdio> 9 #include <algorit

poj 2251 Dungeon Master(简单三维广搜)

题意: 之前做过的所有题都是   在一个平面   上搜索 . 本题很新,在一个三维空间里 ,首先   l  x  y   分别代表 l 层   每一层有 x 行   y 列 问从 S 开始   走到 E   最小步是多少    显然用广搜,只是多了一个向上向下的搜索. 注意: 所谓广搜  ,是一层一层的搜,  每一层其步数是一样的   ,可以通过建立一个数组存步数 ,也可以 将步数同时存入队列中 另外搜过的要做标记, 在做标记时  尽量要在里面做标记(就是每搜过一个点就  立马 将之标记)不然一