CodeForces - 198B Jumping on Walls

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

Example
Input
7 3
---X--X
-X--XX-
Output
YES
Input
6 2
--X-X-
X--XX-
Output

NO

题意:就是悬崖有两条路,X不能走,—能走,可以在一边,往上走,或者往下走,跳到对面要往上走k步,同时,你每次动一下,水位就上涨一米。

用了广搜,而且要标记走过的路,重要的是,注意范围,最远能跳到2*10^5.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

struct node {
    int stemp;
    int x,y;
} D,d;
queue<node>q;
char Map[4][201005];
int book[4][201005];

int main() {
    int n,k;

    int next[4][2]= { {0,1},{0,-1},{1,1},{-1,1}};
    int i,j,f;
    while(scanf("%d%d",&n,&k)!=EOF) {
        memset(Map,0,sizeof(Map));
        memset(book,0,sizeof(book));
        getchar();
        for(i=1;i<=2;i++){
            for(j=1;j<=n;j++)
            // scanf("%c",&Map[i][j]);
            cin>>Map[i][j];
             getchar();
        }

        D.stemp=0;
        D.x=1;
        D.y=1;
        book[D.x][D.y]=1;

        while(!q.empty()) q.pop();
        q.push(D);
      //  printf("R D.x=%d D.y=%d D.stemp=%d\n",D.x,D.y,D.stemp);
        f=0;
        while(!q.empty()) {
            D=q.front();
     //   cout << "ok" << endl;
            q.pop();
          //  printf("C D.x=%d D.y=%d D.stemp=%d\n",D.x,D.y,D.stemp);
            if(D.y>=n) {
                f=1;
                printf("YES\n");
                break;
            }

            for(i=0; i<4; i++) {
                d.stemp=D.stemp+1;
                if(i<2) {
                    d.x=D.x+next[i][0];
                    d.y=D.y+next[i][1];
                } else {
                    d.x=D.x+next[i][0];
                    d.y=D.y+next[i][1]*k;

                }
             //   printf("W d.x=%d d.y=%d d.stemp=%d\n",d.x,d.y,d.stemp);
               // if(d.x<1 || d.x>2 || d.y<1 || Map[d.x][d.y]=='X' || d.y<=d.stemp|| book[d.x][d.y]==1)
               //     continue;
                if(d.x<1 || d.x>2 || d.y<1)//这些不是并列关系,有先后顺序的
                    continue;
                if(Map[d.x][d.y]=='X')
                    continue;
                if(d.y<=d.stemp)
                    continue;
                if(book[d.x][d.y]==1)
                    continue;
                q.push(d);
                book[d.x][d.y]=1;
              //  printf("R d.x=%d d.y=%d d.stemp=%d\n",d.x,d.y,d.stemp);
            }

        }
        if(f==0)
            printf("NO\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值