CodeForces 540C Program D

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

#include<iostream>  
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)

typedef long long LL;
const int INF = (1<<30)-1;const int mod=1000000007;
const int maxn=100005;

char g[505][505];
int n,m,flag;
int st1,st2,en1,en2;
int dir[4][2]={1,0,-1,0,0,1,0,-1};

struct node{
     int x,y;
};

void bfs(){
     node u;
     u.x=st1;u.y=st2;
     queue<node> q;
     q.push(u);

     node v;
     while(!q.empty())   {
         u=q.front();q.pop();

         for(int i=0;i<4;i++)     {
             v.x=u.x+dir[i][0];
             v.y=u.y+dir[i][1];

             if(v.x==en1&&v.y==en2&&g[v.x][v.y]==‘X‘)       {
                 flag=1;
                 return;
            }
             if(v.x<1||v.x>n||v.y<1||v.y>m) continue;
             if(g[v.x][v.y]==‘X‘) continue;

             q.push(v);
             g[v.x][v.y]=‘X‘;
        }
    }    }

int main(){    scanf("%d %d",&n,&m);
   for(int i=1;i<=n;i++)
   for(int j=1;j<=m;j++)
     cin>>g[i][j];
      cin>>st1>>st2;
      cin>>en1>>en2; 

       flag=0;
       bfs();

       if(flag) printf("YES\n");
       else printf("NO\n");
         return 0;
}
时间: 2024-10-12 13:06:21

CodeForces 540C Program D的相关文章

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

(简单广搜) 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

CodeForces 468A Program F

Description Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game. Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote

CodeForces 534D Program B

Description On February, 30th n students came in the Center for Training Olympiad Programmers (CTOP) of the Berland State University. They came one by one, one after another. Each of them went in, and before sitting down at his desk, greeted with tho

「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)

题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是这题...是冰塔的一层 也就是说,它只是个稍微有点限制的二维迷宫问题. 后面就好理解了,不过需要考虑下这种数据: 1 2 XX 1 1 1 1 这种数据答案是no.解决的方法可以考虑这样:分成两个数组来记录访问状态:vis数组和block数组. 代码 #include <queue> #inclu

Codeforces Round #174 (Div. 2)---D. Cow Program(dp, 记忆化搜索)

Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1,?a2,?-,?an of positive integers: Initially, x?=?1 and y?=?0. If, after any step, x

Codeforces 283B Cow Program

题意:给你a[2] - a[n], 一个初始x = 1,初始y = 0  ,执行下面步骤 1) x +=a[x] ,y += a[x] 2) x -= a[x] ,y += a[x] 3)重复1-2步骤.只要中间x <=0 || x > n  就跳出.输出y的值 问你a[1] 从 [1,n-1]分别的值为多少. 解题思路:dfs 求 dp[i][0/1]  求以加法进入 i 和以减法进入 i 能得到的值. 解题代码: 1 // File Name: 283b.cpp 2 // Author:

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f