Arrow Path CodeForces - 1948C
题目位置 CodeForces 1948C
简单 bfs,为什么标签上面是 dp 呢
注意事项
- 多测不清空,爆零两行泪
- 题目中是两个操作为一步,这两个操作都是需要记录在判重数组里面的
- 边界问题
注意边界条件。
#include <stdio.h>
#include <functional>
#include <utility>
#include <queue>
#include <string.h>#define FOR(i, a, b) for (int i = (a); i < (b); i++)const int N = 2e5 + 10;
char Map[2][N];
int vis[2][N];void Solve()
{int n;scanf("%d", &n);FOR(i, 0, 2) FOR(j, 0, n) scanf(" %c", &Map[i][j]);auto bfs = [&]() {std::pair<int, int> pos[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};std::queue<std::pair<int, int>> q;memset(vis, 0, sizeof(vis));vis[1][1] = 1, q.push({1, 1});while (q.size()) {auto [fx, fy] = q.front(); q.pop();if (fx == 1 && fy == n - 1) return true;for (auto [x, y] : pos) {int tx = fx + x, ty = fy + y;if (tx >= 0 && ty >= 0 && tx < 2 && ty < n && !vis[tx][ty]) {vis[tx][ty] = 1;ty = (Map[tx][ty] == '>') ? ty + 1 : ty - 1;if (!vis[tx][ty]) q.push({tx, ty}), vis[tx][ty] = 1;} }}return false;};if (bfs()) printf("Yes\n");else printf("nO\n");
}int main()
{int kase;scanf("%d", &kase);while (kase --) Solve();return 0;
}