QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#719091 | #8981. Kangaroo Puzzle | KobicGend | TL | 0ms | 0kb | C++23 | 2.1kb | 2024-11-06 22:27:21 | 2024-11-06 22:27:24 |
answer
#include <bits/stdc++.h>
using namespace std;
const string dirs = "DRUL";
const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, 1, 0, -1 };
int main() {
int n, m;
cin >> n >> m;
vector mp(n, vector(m, -1));
vector<int> kangaroo;
for (int x = 0; x < n; x++) {
string s;
cin >> s;
for (int y = 0; y < m; y++) {
if (s[y] == '1') {
mp[x][y] = kangaroo.size();
kangaroo.push_back(mp[x][y]);
}
}
}
vector<array<int, 4>> adj(kangaroo.size());
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if (mp[x][y] == -1) continue;
for (int dir = 0; dir < 4; dir++) {
auto [nx, ny] = pair(x + dx[dir], y + dy[dir]);
if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
adj[mp[x][y]][dir] = mp[x][y];
} else if (mp[nx][ny] == -1) {
adj[mp[x][y]][dir] = mp[x][y];
} else {
adj[mp[x][y]][dir] = mp[nx][ny];
}
}
}
}
queue<array<int, 2>> Q;
vector dis(kangaroo.size(), vector(kangaroo.size(), -1));
for (int u = 0; u < kangaroo.size(); ++u) {
dis[u][u] = 1;
Q.push({ u, u });
}
while (!Q.empty()) {
auto [x, y] = Q.front();
Q.pop();
for (int dir = 0; dir < 4; dir++) {
for (auto nx : { x, adj[x][dir ^ 2] }) {
if (adj[nx][dir] != x) continue;
for (auto ny : { y, adj[y][dir ^ 2] }) {
if (adj[ny][dir] != y) continue;
if (dis[nx][ny] == -1) continue;
dis[nx][ny] = dir;
Q.push({ nx, ny });
}
}
}
}
string res;
while (kangaroo.size() > 1) {
int x = -1, y = -1;
for (auto s : kangaroo) {
for (auto t : kangaroo) {
if (s != t && dis[s][t] != -1) {
x = s, y = t;
}
}
}
while (x != y) {
int dir = dis[x][y];
res += dirs[dir];
x = adj[x][dir];
y = adj[y][dir];
for (int& z : kangaroo) {
z = adj[z][dir];
}
}
sort(kangaroo.begin(), kangaroo.end());
kangaroo.erase(unique(kangaroo.begin(), kangaroo.end()), kangaroo.end());
}
cout << res << endl;
return 0;
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
4 4 1111 1001 1001 1110