旋转水管https://codeforces.com/gym/103941/problem/H旋转水管 爆搜,回溯时恢复现场即可。
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
void inline TLE() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); }
int m, Y, YY;
bool st[4][500050];
string s[3];
bool dfs(int x, int y, int p) {
if (x == 3 && y == YY)return true;
if (st[x][y] || x > 2 || x < 1 || y > m || y < 1)return false;
st[x][y] = true;
if (s[x][y] == 'I') {
if (dfs(x + p, y + (p ^ 1), p))return true;
if (p == 0 && dfs(x + p, y - (p ^ 1), p))return true;
}
else if (s[x][y] == 'L') {
if (dfs(x + (p ^ 1), y + p, (p ^ 1)))return true;
if (dfs(x - (p ^ 1), y - p, (p ^ 1)))return true;
}
st[x][y] = false;
return false;
}
void solve() {
cin >> m >> Y >> YY;
for (int i = 1;i <= m;i++)st[1][i] = 0, st[2][i] = 0;
for (int i = 1;i <= 2;i++) {
s[i].clear();
cin >> s[i];
s[i] = "/" + s[i];
}
if (dfs(1, Y, 1))cout << "YES" << endl;
else cout << "NO" << endl;
}
signed main() {
TLE();
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}