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 kmeters 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.
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.
Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes).
7 3 ---X--X -X--XX-
YES
6 2 --X-X- X--XX-
NO
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.
题意:这算是一个忍者攀爬的游戏吧,两个字符串代表两边的悬崖,其中‘-’可以攀爬,‘X’不可以,有三种操作,可以在一边的悬崖向上或者向下走一步,或者跳到另外一个悬崖上,但是跳跃的高度为k,比如说在左边的悬崖的高度为3,并且每操作一步,悬崖下的水位就会上升一米,人的高度必须高于水位线,那么跳到右边高度就是3+k,如果3+k>n 表示跳出悬崖就是YES,因为字符比作悬崖不易懂,我解释一下第一组样例:
7 3 ---X--X 代表左边悬崖 -X--XX- 代表右边悬崖
左 顶部 右
X | 7 | - |
- | 6 | X |
- | 5 | X |
X | 4 | - |
- | 3 | - |
- | 2 | X |
- | 1 | - |
注意的点就是,人的高度必须高于水位线,并且走过的地点不能再走了,否则会超时。
我用了算是最笨的方法吧,不过很容易懂。
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf 1<<30
#define eps 1e-10
#define maxn 100005
#define zero(a) fabs(a)<eps
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson step<<1
#define rson step<<1|1
#define MOD 1000000009
#define sqr(a) ((a)*(a))
using namespace std;
int n, k, flag;
struct wall{
char left;
char right;
int x , y;
}w[maxn];
void dfs(int water, int loc, int fr) {
if(loc <= water) return ;
if(fr == 0) {
if(w[loc].x) return;
if(flag == 0) return ;
if(loc+k==n&&w[n].right == '-') {
flag = 0;
return ;
}
if(loc+k>n) {
flag = 0;
return ;
}
w[loc].x = 1;
if(w[loc+k].right == '-')
dfs(water+1,loc+k,1);
if(loc > 1 && w[loc-1].left == '-')
dfs(water+1,loc-1,0);
if(w[loc+1].left == '-')
dfs(water+1,loc+1,0);
}
else {
if(w[loc].y) return ;
if(flag == 0) return ;
if(loc+k==n && w[n].left == '-') {
flag = 0;
return ;
}
if(loc+k>n) {
flag = 0;
return ;
}
w[loc].y = 1;
if(w[loc+k].left == '-')
dfs(water+1,loc+k,0);
if(loc > 1 && w[loc-1].right == '-')
dfs(water+1,loc-1,1);
if(w[loc+1].right == '-')
dfs(water+1,loc+1,1);
}
return ;
}
int main() {
while(~scanf("%d%d",&n,&k)) {
flag = 1;
for(int i = 1; i <= n; i++)
cin>>w[i].left;
for(int i = 1; i <= n; i++)
cin>>w[i].right;
dfs(0,1,0);
if(flag == 0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}