ICE CAVE (BFS Search (simulation))

Source: Internet
Author: User


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

Title, every step, go through that place into a broken ice, if you go again will fall, you can not beat in situ to make the ice change state, and your final goal is not just to find the destination, and to make it into X and then re-enter

Originally I wanted to think about the situation, such as the destination is X or. , and then judge the condition is changed, and then count, mark is not a problem, and consider from the origin to the origin ...

The final mess, to consider too much, the problem is very complex, in fact, as long as the conditions will arrive at that point on the map whether it is X, the rest of the simulation implementation, go to a point, that point becomes x ...

#include"Iostream"#include"Algorithm"#include"CString"#include"Cstdio"#include"Queue"UsingNamespace Std;Constint MAXN=510;Char A[MAXN][maxn];int mN, book[MAXN][maxn];int NEX[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int SPSqPQTpTq;struct Node{int x;int y;};IntBFS(){Queue<struct Node>que;struct Node CET; c. x=sp; c. Y=sq; que.Push(c);While(!que.Empty()){E=que.Front(); Que.Pop();For(int k=0; k<=3; k++){T. x=e. x+nex[k][0]; T. Y=e. Y+nex[k][1];If(t. x<0|| T. x>=m|| T. Y<0|| T. Y>=n)Continue;If(t. x==p&&t. Y==q&&a[t. x][t. Y]==X){Return1;}ContinueBook[t.x][t.y]=1;If(A[t. x][t. Y]==X)Continue; A[t. x][t. Y]=X; Que.Push(t);}}Return0;}IntMain(){While(CIN>>m>>n&&m){For(int I=0; I<m; I++){scanf('%s 'A[I]);}Memset(Book,0,sizeof(Book)); Cin>>sp>>sq; Cin>>p>>q; Sp=sp-1=sq-1 p< Span class= "Sh-symbol" >=p-1 Q=q-1if (bfs ()) Cout<< "YES" <<endlelse cout<< "NO" <<endl; }return 0;< Span class= "Sh-cbracket" >             

ICE CAVE (BFS Search (simulation))

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.