QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#729976#9564. Hey, Have You Seen My Kangaroo?ucup-team5062WA 0ms20172kbC++203.8kb2024-11-09 18:10:492024-11-09 18:10:49

Judging History

你现在查看的是最新测评结果

  • [2024-11-09 18:10:49]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:20172kb
  • [2024-11-09 18:10:49]
  • 提交

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] != -1) break;
			}
			if (Visited[cx] == i) {
				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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 20172kb

input:

3 3 6
ULDDRR
010
111
010

output:

8
4
2
1
0
0
0
0
0

result:

wrong answer 1st numbers differ - expected: '-1', found: '8'