TLE求助

P10234 [yLCPC2024] B. 找机厅

看不到代码
by _zuoqingyuan @ 2024-03-16 22:08:20


@[_zuoqingyuan](/user/731650) ``` #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cstring> using namespace std; typedef long long l; l t, n, m, x, y, c, tx, ty, ax[] = {0, 1, -1, 0, 0}, ay[] = {0, 0, 0, 1, -1}; char ch[] = {'0', 'D', 'U', 'R', 'L'}; string s; char a[2005][2005]; struct Node { l x, y, cnt; string s; }; int main() { scanf("%lld", &t); while (t--) { bool vis[2005][2005], f = 0; scanf("%lld%lld", &n, &m); for (l i = 1; i <= n; ++i) { for (l j = 1; j <= m; ++j) { cin >> a[i][j]; vis[i][j] = 0; } } queue<Node> q; q.push({1, 1, 0, ""}); while (!q.empty()) { x = q.front().x; y = q.front().y; c = q.front().cnt; vis[x][y] = 1; if (x == n && y == m) { printf("%lld\n", c); cout << q.front().s; printf("\n"); f = 1; break; } for (l i = 1; i <= 4; ++i) { l tx = x + ax[i], ty = y + ay[i]; if (tx <= n && tx > 0 && ty <= m && ty > 0 && a[x][y] != a[tx][ty] && !vis[tx][ty]) { q.front().s.push_back(ch[i]); q.push({tx, ty, c + 1, q.front().s}); q.front().s.pop_back(); } } q.pop(); } if (!f) printf("-1\n"); } return 0; } ```
by small_Dongpo @ 2024-03-20 22:04:02


|