Test instructions: A picture of n*2, ' X ' means not to go, '-' stands for walking. A ninja (0,1) starts, there are three ways to jump, jump to the opposite side of the K-step, up or down one step, each jump, the flood will go up a grid, ask the ninja can jump out of this picture.
The most critical: BFS and DFS are available, DFS needs to optimize the search
Code
BFs
#include <cstdio> #include <queue> #define MAX 100010 using namespace std;
struct node {int flag;
int cur;
int flood;
Node () {} node (int x = 0, int y = 0, int z = 0) {flag = x;
cur = y;
Flood = Z;
}
};
int n, K;
Char A[5][max];
BOOL Vis[5][max] = {false};
Queue<node> Q;
BOOL BFs () {struct node E (0, 1, 0);
Vis[e.flag][e.cur] = true;
Q.push (e);
while (!q.empty ()) {struct node F = q.front ();
Q.pop ();
struct node tmp (F.flag, f.cur-1, F.flood + 1); if (!vis[tmp.flag][tmp.cur] && a[tmp.flag][tmp.cur]! = ' X ' && tmp.cur > Tmp.flood && tmp.cur &G T
0) Q.push (TMP), vis[tmp.flag][tmp.cur] = true;
struct node tmp1 (f.flag, F.cur + 1, F.flood + 1);
if (Tmp1.cur > N) return true; if (!vis[tmp1.flag][tmp1.cur] && a[tmp1.flag][tmp1.cur]! = ' X ' && tmp1.cur > Tmp1.flood &&
Tmp1.cur > 0) q.push (TMP1), vis[tmp1.flag][tmp1.cur] = true; struct node tmp2 (f.flag ^ 1, f.cur + K, F.floOD + 1);
if (Tmp2.cur > N) return true; if (!vis[tmp2.flag][tmp2.cur] && a[tmp2.flag][tmp2.cur]! = ' X ' && tmp2.cur > Tmp2.flood &&
Tmp2.cur > 0) q.push (TMP2), vis[tmp2.flag][tmp2.cur] = true;
} return false;
} int main () {scanf ("%d%d", &n, &k);
scanf ("%s%s", A[0] + 1, a[1] + 1);
if (BFS ()) printf ("yes\n");
else printf ("no\n");
return 0; }
Dfs:
#include <cstdio>
#include <cstring>
#define MAX 100010
Char A[max];
Char B[max];
int n, K;
BOOL Vis[5][max] = {false};
BOOL Dfs (int flag, int cur, int flood)
{
if (cur <= flood) return false;
if (cur <= 0) return false;
if (cur > N) return true;
if (flag = = = 0 && a[cur] = = ' X ') return false;
if (flag = = = 1 && b[cur] = = ' X ') return false;
if (!vis[flag][cur])
{
Vis[flag][cur] = true;
if (DFS (flag ^ 1, cur + k, flood + 1)) return true;;
if (DFS (flag, cur + 1, flood + 1)) return true;
if (DFS (flag, cur-1, flood + 1)) return true;
return false;
}
int main ()
{
scanf ("%d%d", &n, &k);
scanf ("%s", a + 1);
scanf ("%s", B + 1);
if (Dfs (0, 1, 0)) printf ("yes\n");
else printf ("no\n");
return 0;
}