您的位置:首页 > 其它

Codeforces-199D:Jumping on Walls(DFS+思维)

2018-01-05 11:03 519 查看
D. 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 n characters. 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
7 3
---X--X
-X--XX-


output
YES


input
6 2
--X-X-
X--XX-


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

思路:因为每个格子最多经过一次,直接DFS从起点开始遍历。但是要注意DFS的顺序:由于有洪水的限制,所以应该优先选择“jump to the opposite wall”这个操作,这样就能保证在洪水淹没之前到达目标位置。

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6;
char a[MAX],b[MAX];
int v[2][MAX];
int n,m;
int dfs(int tag,int k,int h)
{
if(k<=h||k<=0)return 0;
if(k>n)return 1;
if(tag==0&&a[k]=='X')return 0;
if(tag==1&&b[k]=='X')return 0;
if(v[tag][k])return 0;
v[tag][k]=1;
if(dfs(tag^1,k+m,h+1))return 1;//优先选择"jump to the opposite wall"
if(dfs(tag,k+1,h+1))return 1;
if(dfs(tag,k-1,h+1))return 1;
return 0;
}
int main()
{
cin>>n>>m;
scanf("%s%s",a+1,b+1);
memset(v,0,sizeof v);
puts(dfs(0,1,0)?"YES":"NO");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: