[BFS, flood title] Codeforces 198B Jumping on Walls

Topic: http://codeforces.com/problemset/problem/198/B

Jumping on Walls
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon.

The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous.

Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions:

  • climb one area up;
  • climb one area down;
  • jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall.

If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon.

The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on.

The level is considered completed if the ninja manages to get out of the canyon.

After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly.

The second line contains the description of the left wall — a string with the length of ncharacters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area.

The third line describes the right wall in the same format.

It is guaranteed that the first area of the left wall is not dangerous.

Output

Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes).

Examples
input
Copy
7 3
---X--X
-X--XX-
output
Copy
YES
input
Copy
6 2
--X-X-
X--XX-
output
Copy
NO
Note

In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon.

In the second sample there's no way the ninja can get out of the canyon.

Meaning of the questions:

A man block between two parallel walls, each wall is divided into n units wall, the wall can not reach some locations, people may be moved, but the water level rises every movement, can not hit the water, now the initial position in the bottom left is the upper left corner of the sample, if the people of the current height of x, then each person can perform three operations,
the first is a rise in the current wall unit that is x + 1, the second a decline in the current wall and another wall unit x-1, and the third is jumping height becomes x + k, asked whether people jump out of the wall (human height strictly greater than is strictly greater than n !!! Note , which is not equal to) not flooded

Ideas:

A very flood water search problem, really want to Tucao about when the game did not want to understand the problem, he wrote the judge wrote condition y + k> = n in the game, but may k == 1 led to a jump grid after the water rises just a grid, it is cool, after the game into y + k> n on the AC, really want to give yourself a slap at the time
by the way, pay attention to the character of the first movement of the water will rise, so to be able to judge can not jump out of the water there is no longer determined to rise to the level where people

note:

Q. Can people out of the wall (human height strictly greater than is strictly greater than n !!! Note, that is not equal to) not flooded

People first moved the water will rise, so the judge can not jump out first and then determine the water has not risen to the level where people

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=1e5+5;
 4 #define fi first
 5 #define se second
 6 int n,k,mp[5][amn],hm[5][amn];bool valid,idx[5][amn];
 7 struct pii{
 8     int first,second,h;
 9     pii(int f,int s,int hh){first=f,second=s,h=hh;}
10 };
11 void bfs(){
12     memset(idx,0,sizeof idx);
13     queue<pii> q;
14     idx[0][1]=1;
15     q.push(pii(0,1,0));
16     while(q.size()){
17         int x=q.front().first,y=q.front().second,h=q.front().h;q.pop();
18         if(y+k>n){valid=1;return;}  ///When the game did not want to understand the problem, he wrote the judge wrote condition y + k> = n in the game, but may k == 1 led to a rise in just jumped grid after a grid of water, to cool after the game into y + k> n on the AC, and then really want to own a slap 
. 19          IF (Y <= H) { Continue ;}          /// this is a first jump water will rise, so the determination can not jump to out and then determine the water has not risen to the level of people where 
20 is          IF (IDX [X] [Y +! . 1 ] && MP [X] [Y + . 1 {IDX [X] [Y +]) . 1 ] = . 1 ; q.push (PII (X, Y + . 1 , H + . 1 ));}
 21 is          IF (IDX [X] [Y-! . 1 ] && MP [X] [Y- . 1 ]) {IDX [X] [Y- . 1 ] = . 1 ; Q. Push (PII (X, Y- . 1 , H + . 1 ));}
 22 is          IF (IDX [X ^! . 1][y+k]&&mp[x^1][y+k]){idx[x^1][y+k]=1;q.push(pii(x^1,y+k,h+1));}
23     }
24 }
25 int main(){
26     ios::sync_with_stdio(0);
27     cin>>n>>k;
28     char in;
29     for(int i=1;i<=n;i++){
30         cin>>in;
31         if(in=='-')mp[0][i]=1;
32         else mp[0][i]=0;
33     }
34     for(int i=1;i<=n;i++){
35         cin>>in;
36         if(in=='-')mp[1][i]=1;
37         else mp[1][i]=0;
38     }
39     valid=0;
40     BFS ();
 41 is      IF (Valid =! 0 ) the printf ( " YES \ n- " );
 42 is      the else the printf ( " NO \ n- " ;)
 43 is  }
 44 is  / * *
 45  between a man in the wall of two block parallel, each wall is divided into n units wall, the wall can not reach some locations, people can be moved, but each time moving the water level will rise, people can not hit the water, the initial position of the bottom of the left side now, is in the sample the upper left corner, if the height of the current human x, then each person can perform three operations,
 46  a first current rise in a wall unit i.e. x + 1, the second current is decreased by one unit and the wall x- 1, the third is jumping another wall and height becomes x + k, asked whether people jump out of the wall (human height strictly greater than is strictly greater than n !!! Note, that is not equal to) and not by water submerged
 47  a very flood water search problem, really want to Tucao about when the game did not want to understand the problem, he wrote the judge wrote condition y + k> = n in the game, but may lead to jump k == 1 rising water just after a frame by frame, to cool after the game into y + k> n on the AC, really I want to own a slap in the face to the then
 48  by at the Way, pay attention to the characters moving water will rise, so first judge can not jump out Then there is determined not rise to the water layer where the person
 49 **/

 

Guess you like

Origin www.cnblogs.com/Railgun000/p/11433587.html
Recommended