CodeForces 198B Jumping on Walls(dfs)

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.

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

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



题解:

有两条路,-表示路x表示障碍物,一开始在第一条的起始点,你每一回合水位上升一个单位,问你最后能否逃出这个地方

你可以选择3种操作:前进一步,后退一步,当前位置为x,跳到另一条路上的x+k位置

思路:

三种操作直接dfs,不过要记忆化一下不然会tle

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
char s[2][100005];
int vis[2][100005];
int len,k,tag;
void dfs(int x,int y,int d)
{
    //printf("x=%d y=%d d=%d!\n",x,y,d);
    vis[x][y]=1;
    if(y+k>len||tag)
    {
        tag=1;
        return;
    }
    int t=!x;
    if(s[t][y+k]=='-'&&!vis[t][y+k])
    {
        dfs(t,y+k,d+1);
    }
    if(tag)
        return;
    if(y-2>d)
    {
        if(s[x][y-1]=='-'&&!vis[x][y-1])
            dfs(x,y-1,d+1);
    }
    if(tag)
        return;
    if(s[x][y+1]=='-'&&!vis[x][y+1])
        dfs(x,y+1,d+1);
}
int main()
{
    int i,j;
    scanf("%d%d",&len,&k);
    memset(vis,0,sizeof(vis));
    len--;
    tag=0;
    for(i=0;i<2;i++)
        scanf("%s",s[i]);
    dfs(0,0,-1);
    if(tag)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}
本站公众号
   欢迎关注本站公众号,获取更多程序园信息
开发小院