Pipes
按题意模拟就行
根据当前位置、水流来的方向以及管道类型,可以通往的位置和方向是固定的
dfs一步步走
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, mid, rt << 1
#define rrt mid + 1, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x7f7f7f7f;
const double eps = 1e-5;
const double Pi = acos(-1);
const int maxn = 2e5 + 5;
int n, flag;
char an[2][maxn];
//1 2 3 4分别为水流来的方向左 上 右 下
void dfs(int x, int y, int from) {
if (y > n) {
if (x == 1 && from == 1)
flag = 1;
return;
}
if (x == 0 && from == 1) {
if (an[x][y] <= '2')
dfs(x, y + 1, 1);
else
dfs(x + 1, y, 2);
} else if (x == 0 && from == 4) {
if (an[x][y] > '2')
dfs(x, y + 1, 1);
} else if (x == 1 && from == 1) {
if (an[x][y] <= '2')
dfs(x, y + 1, 1);
else
dfs(x - 1, y, 4);
} else if (x == 1 && from == 2) {
if (an[x][y] > '2')
dfs(x, y + 1, 1);
}
}
int main(void) {
int T;
read(T);
while (T--) {
read(n);
cin >> an[0] + 1 >> an[1] + 1;
flag = 0;
dfs(0, 1, 1);
if (flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}