Ice Cave

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

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

题目大意:

m*n的冰宫,'X'代表破损,'.'代完好,你初始的位置为(r1, c1),因为你是从上一层掉下来的,所以初始位置为'X'是破损的,你要从(r2, c2)的位置穿到下一层

你走过的地方冰块都会破损,所以处理(r2, c2)外是'X'的你都不能走,但(r2,c2)这个点你需要走两次,第一次为了破损他让他变为'X',第二次到他是通过他到下一层

BFS搜索,DFS会TLE

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm> using namespace std;
const int N = ;
typedef __int64 ll; char maps[N][N];
int d[][] = {{, }, {-, }, {, }, {, -}};
int m, n, f, r2, c2; struct node
{
int x, y;
}; bool BFS(int x, int y)
{
queue<node>Q;
node now, next;
now.x = x;
now.y = y;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
for(int i = ; i < ; i++)
{
int a = next.x = now.x + d[i][];
int b = next.y = now.y + d[i][];
if(a == r2 && b == c2 && maps[a][b] == 'X')
return true;
if(a >= && a < m && b >= && b < n && maps[a][b] == '.')
{
if(maps[a][b] == '.')
maps[a][b] = 'X';//走过的地方,冰块会破损,将其变为'X'
Q.push(next);
}
}
}
return false;
} int main()
{
int r1, c1;
while(~scanf("%d%d", &m, &n))
{
f = ;
for(int i = ; i < m ; i++)
scanf("%s", maps[i]);
scanf("%d%d", &r1, &c1);
scanf("%d%d", &r2, &c2);
r1--;
c1--;
r2--;
c2--;
if(BFS(r1, c1))
printf("YES\n");
else
printf("NO\n");
}
return ;
}

CodeForces 540C Ice Cave (BFS)的更多相关文章

  1. Codeforces 301_div.2_Ice Cave(BFS走冰块)

    Ice Cave Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Descripti ...

  2. CodeForces - 540C Ice Cave —— BFS

    题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...

  3. ICE CAVE(BFS搜索(模拟))

    Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...

  4. CodeForces 540C Ice Cave (BFS)

    题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...

  5. codeforces 1283D. Christmas Trees(bfs)

    链接: https://codeforces.com/contest/1283/problem/D 题意:给定n个不同的整数点,让你找m个不同的整数点,使得这m个点到到这n个点最小距离之和最小. 思路 ...

  6. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  7. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  8. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  9. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

随机推荐

  1. 搜索旋转排序数组 II

    跟进“搜索旋转排序数组”,假如有重复元素又将如何? 一句话思路:不能二分,因为复杂度是n eg全是0,找一个1 class Solution { public boolean search(int[] ...

  2. XAMPP下apache部署网站,多个虚拟机(空间)配置

     之 前在 Mac 下一直使用 MAMP 搭建本地 php 环境,因为其强大的组合配置以及方便的 UI 操作.因为公司的电脑是 Windows 的,有时候需要搭建 php 环境来调试一些小东西方便自己 ...

  3. 【转】HttpHandler的认识与加深理解

    原文:http://www.cnblogs.com/whtydn/archive/2009/10/19/1585778.html HttpHandler是HTTP请求的处理中心,真正地对客户端请求的服 ...

  4. 【SQL模板】四.插入/更新 列模板TSQL

    ---Name: 插入/更新 列模板.sql ---Purpose: 用于更新 数据库中 列 的脚本模板 ---Author: xx ---Time: 2015-12-18 10:26:06 ---R ...

  5. [Sikuli] Sikuli安装

    http://blog.csdn.net/defectfinder/article/details/49819215 1.下载 sikulixsetup-1.1.0.jar (md5, sig) ht ...

  6. Java 8 Optional 类深度解析

    Java 8 Optional 类深度解析 身为一名Java程序员,大家可能都有这样的经历:调用一个方法得到了返回值却不能直接将返回值作为参数去调用别的方法.我们首先要判断这个返回值是否为null,只 ...

  7. const stirng* 类型的指针cp,当cout<<*cp<<endl:会提示没有与之匹配的“<<”运算符

    #include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> usin ...

  8. 2018.10.08 NOIP模拟 斐波那契(贪心+hash/map)

    传送门 签到题. 显然是可以贪心分组的,也就是尽量跟当前的分成一组. 这时我们需要判断a[l]+a[r],a[l+1]+a[r]...a[r−1]+a[r]a[l]+a[r],a[l+1]+a[r]. ...

  9. 2018.07.01 BZOJ3295: [Cqoi2011]动态逆序对(带修主席树)

    3295: [Cqoi2011]动态逆序对 **Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j& ...

  10. 2018.09.09 UVa10529 - Dumb Bones(期望dp)

    传送门 期望dp好题. f[i]表示摆放i个的最小花费,于是f[i]可以从f[j]与f[i-j+1]转移过来了. 代码: #include<bits/stdc++.h> #define N ...