QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#719091#8981. Kangaroo PuzzleKobicGendTL 0ms0kbC++232.1kb2024-11-06 22:27:212024-11-06 22:27:24

Judging History

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

  • [2024-11-06 22:27:24]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-06 22:27:21]
  • 提交

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

output:


result: