Codeforces 540C Ice Cave (BFS)

Source: Internet
Author: User

Http:// Ice CaveTime limit:2000MS Memory Limit:262144KB 64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 540C


You play a computer game. Your character stands on some level of a multilevel ice cave. In order-to-move on forward, you need-descend one level lower and the-only-to-do-is-to-fall through the ice.

The level of the cave where you are a rectangular square grid of  n  rows an D  m  columns. Each cell consists the either from intact or from cracked ice. From each cell you can move to cells that is side-adjacent with yours (due to some limitations of the game engine can Do 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 your move to the cell with INTAC T ice, then the ice in 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 were staying in the cell (R1, c1) and this cell was cracked because you ' ve just fallen her E from a higher level. You need to fall down through the cell (R2, C2) since the exit to the next level is there. C A you does this?


The first line contains and integers, n and m (1≤ n, m ≤500) -the number of rows and columns in the cave description.

Each of the Next   lines describes the initial state of the" the "the Cave , each line consists Of  m  characters "." (that is, intact ice) and "x" (cracked ice).

The next line contains Integers,  R 1 and   C 1  (1≤ R Sub class= "Lower-index" >1≤ n , 1≤ c 1≤ m )  -your initial Coordi Nates. It is guaranteed that the description of the cave contains character ' x ' in Cell  ( R 1, C 1), that's, the ice on The starting cell is initially cracked.

The next line contains integers r2 and C2 (1≤ r2≤ n, 1≤ C 2≤ m)-the coordinates of the cell through which you need to fall. The final cell may be coincide with the starting one.


If you can reach the destination, print 'YES ', otherwise print 'NO '.

Sample Input

4 6
X... Xx
... Xx.
. X.. X.
1 6
2 2
5 4
. X..
... X
. Xx.
5 3
1 1
4 7
.. x.xx.
. Xx.. X.
X... X..
2 2
1 6

Main topic:

M*n's Ice Palace, ' X ' stands for breakage, '. ' In good condition, your initial position is (R1, C1), because you are falling from the previous layer, so the initial position is ' X ' is broken, you have to go from the position (R2, C2) to the next layer

The place where you walk through the ice will be broken, so the treatment (R2, C2) outside the ' X ' You can not go, but (R2,C2) this point you need to walk two times, the first time in order to break he let him into the ' X ', the second to him is through him to the next layer

BFS Search, DFS will tle

#include <stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<queue>#include<stack>#include<algorithm>using namespacestd;Const intN =510; typedef __int64 LL;CharMaps[n][n];intd[4][2] = {{1,0}, {-1,0}, {0,1}, {0, -1}};intm, N, F, R2, C2;structnode{intx, y;};BOOLBFS (intXinty) {Queue<node>p;    Node now, next; now.x=x; Now.y=y;    Q.push (now);  while(!Q.empty ()) { Now=Q.front ();        Q.pop ();  for(inti =0; I <4; i++)        {            intA = Next.x = now.x + d[i][0]; intb = Next.y = Now.y + d[i][1]; if(A = = R2 && b = = C2 && Maps[a][b] = ='X')                return true; if(A >=0&& a < m && b >=0&& b < n && maps[a][b] = ='.')            {                if(Maps[a][b] = ='.') Maps[a][b]='X';//where it goes, the ice breaks and turns it into ' X ' .Q.push (next); }        }    }    return false;}intMain () {intR1, C1;  while(~SCANF ("%d%d", &m, &N)) {f=0;  for(inti =0; I < m; i++) scanf ("%s", Maps[i]); scanf ("%d%d", &AMP;R1, &C1); scanf ("%d%d", &AMP;R2, &C2); R1--; C1--; R2--; C2--; if(BFS (R1, C1)) printf ("yes\n"); Elseprintf ("no\n"); }    return 0;}

Codeforces 540C Ice Cave (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.