Jumping on Walls CodeForces - 199D DFS

题意:

有两行字符串,X代表不能走,初始时在左上角,一个人走一步后,洪水向右移动一列,小于等于洪水时会被淹没,问能不能走出这两行字符串。有三种走法:向左、向右、跳到另一个字符串的当前位置+k处。

思路:

DFS搜索三个走法,看能不能到终点,注意要标记一下走过的点。

代码:

#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#include<string.h>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
const int maxn=100000+66;
#define mod 1000000007
#define INF 0x3f3f3f
int n,k;
char a[6][maxn];
bool vis[6][maxn];
bool dfs(int raw,int col,int water)
{
    if(col>n)
        return 1;
    if(col<=water)
        return 0;
    if(a[raw][col]=='X')
        return 0;
    if(vis[raw][col])
        return 0;
    vis[raw][col]=1;
    bool ok=dfs(raw^1,col+k,water+1);
    if(ok)
        return 1;
    ok=dfs(raw,col+1,water+1);
    if(ok)
        return 1;
    ok=dfs(raw,col-1,water+1);
    if(ok)
        return 1;
    //vis[raw][col]=0;
    return ok;
}
int main()
{
    while(~scanf("%d %d",&n,&k))
    {
        scanf("%s %s",a[0]+1,a[1]+1);
        int ans=dfs(0,1,0);
        if(ans)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

Jumping on Walls CodeForces - 199D DFSJumping on Walls CodeForces - 199D DFS Preeee 发布了991 篇原创文章 · 获赞 63 · 访问量 8万+ 他的留言板 关注
上一篇:程序员需要达到什么水平才能拿到20K?


下一篇:CF1146D Frog Jumping