QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#729975 | #9564. Hey, Have You Seen My Kangaroo? | ucup-team5062 | WA | 4ms | 19000kb | C++20 | 3.8kb | 2024-11-09 18:10:00 | 2024-11-09 18:10:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int Answer[1 << 18];
string C[1 << 18];
string S;
// Other than input
bool Used[1 << 18];
bool Cycle[1 << 18];
int P[1 << 18];
int Dist[1 << 18];
int Count[1 << 18];
vector<int> G[1 << 18];
// Simulation
int V1[1 << 18];
int V2[1 << 18];
int Visited[1 << 18];
int GetNext(int px, int py, char c) {
if (c == 'U') {
if (px >= 1 && C[px - 1][py] == '1') px--;
}
if (c == 'D') {
if (px <= H - 2 && C[px + 1][py] == '1') px++;
}
if (c == 'L') {
if (py >= 1 && C[px][py - 1] == '1') py--;
}
if (c == 'R') {
if (py <= W - 2 && C[px][py + 1] == '1') py++;
}
return px * W + py;
}
pair<int, int> Simulation(int px, int py, int cl, int cr) {
for (int i = cl; i < cr; i++) {
int idx = (i >= K ? i - K : i);
if (S[idx] == 'U') {
if (px >= 1 && C[px - 1][py] == '1') px--;
}
if (S[idx] == 'D') {
if (px <= H - 2 && C[px + 1][py] == '1') px++;
}
if (S[idx] == 'L') {
if (py >= 1 && C[px][py - 1] == '1') py--;
}
if (S[idx] == 'R') {
if (py <= W - 2 && C[px][py + 1] == '1') py++;
}
}
return make_pair(px, py);
}
void dfs(int pos) {
Dist[pos] = 0;
for (int to : G[pos]) {
dfs(to);
Dist[pos] = max(Dist[pos], Dist[to] + 1);
}
}
int main() {
int DEBUG = 1;
// Step 1. Input
cin >> H >> W >> K;
if (DEBUG == 1) {
cin >> S;
for (int i = 0; i < H; i++) cin >> C[i];
}
if (DEBUG == 2) {
for (int i = 0; i < K; i++) {
string str = "LRUD";
S += str[rand() % 4];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) C[i] += ('0' + (rand() % 100 < 99 ? 1 : 0));
}
}
for (int i = 1; i <= H * W; i++) Answer[i] = (1 << 30);
// Step 2. Pre Simulation
for (int i = 0; i < H * W; i++) {
if (C[i / W][i % W] == '0') continue;
pair<int, int> v = Simulation(i / W, i % W, 0, K);
V1[i] = i;
V2[i] = v.first * W + v.second;
}
// Step 3. Brute Force
for (int i = 0; i < K; i++) {
for (int j = 0; j <= H * W + 2; j++) Used[j] = false;
for (int j = 0; j <= H * W + 2; j++) Cycle[j] = false;
for (int j = 0; j <= H * W + 2; j++) G[j].clear();
for (int j = 0; j <= H * W + 2; j++) Count[j] = 0;
for (int j = 0; j <= H * W + 2; j++) Visited[j] = -1;
// Simulation
for (int j = 0; j < H * W; j++) {
if (C[j / W][j % W] == '0') continue;
Used[V1[j]] = true;
P[V1[j]] = V2[j];
}
// Get Cycles
for (int j = 0; j < H * W; j++) {
if (Visited[j] != -1) continue;
if (Used[j] == false) continue;
int cx = j, cnt = 0;
while (true) {
Visited[cx] = j;
cx = P[cx];
cnt++;
if (Visited[cx] != j) break;
if (Visited[cx] == j) break;
}
for (int k = 0; k < cnt; k++) {
cx = P[cx];
Cycle[cx] = true;
}
}
for (int j = 0; j < H * W; j++) {
if (Used[j] == false || Cycle[j] == true) continue;
G[P[j]].emplace_back(j);
}
// Get Distance
for (int j = 0; j < H * W; j++) {
if (Cycle[j] == false) continue;
dfs(j);
}
// Get Answer
for (int j = 0; j < H * W; j++) {
if (Used[j] == false) continue;
int dst = Dist[j];
if (Cycle[j] == true) dst = H * W + 1;
Count[dst] += 1;
}
for (int j = H * W + 1; j >= 1; j--) Count[j - 1] += Count[j];
// Update Answer
for (int j = 0; j <= H * W; j++) {
Answer[Count[j]] = min(Answer[Count[j]], j * K + i);
}
for (int j = 0; j < H * W; j++) {
if (C[j / W][j % W] == '0') continue;
V1[j] = GetNext(V1[j] / W, V1[j] % W, S[i]);
V2[j] = GetNext(V2[j] / W, V2[j] % W, S[i]);
}
}
// Step 4. Output
for (int i = 2; i <= H * W; i++) Answer[i] = min(Answer[i], Answer[i - 1]);
for (int i = 1; i <= H * W; i++) {
if (Answer[i] == (1 << 30)) printf("-1\n");
else printf("%d\n", Answer[i]);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 18352kb
input:
3 3 6 ULDDRR 010 111 010
output:
-1 4 2 1 0 0 0 0 0
result:
ok 9 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 18108kb
input:
3 3 6 ULDDRR 010 111 011
output:
7 4 2 1 1 0 0 0 0
result:
ok 9 numbers
Test #3:
score: -100
Wrong Answer
time: 4ms
memory: 19000kb
input:
1 5 1 R 11111
output:
-1 -1 2 1 0
result:
wrong answer 1st numbers differ - expected: '4', found: '-1'