QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125039#6534. Peg SolitairezyoobnAC ✓6ms3472kbC++201.6kb2023-07-15 23:39:262023-07-15 23:39:29

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-15 23:39:29]
  • 评测
  • 测评结果:AC
  • 用时:6ms
  • 内存:3472kb
  • [2023-07-15 23:39:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

void solve() {
	int n, m, k;
	cin >> n >> m >> k;
	vector<int> x(k), y(k);
	for (int i = 0; i < k; ++i) {
		cin >> x[i] >> y[i];
		--x[i], --y[i];
	}

	vector g(n, vector<bool>(m));
	for (int i = 0; i < k; ++i) {
		g[x[i]][y[i]] = true;
	}
	int ans = k;

	array dx = {1, 0, -1, 0};
	array dy = {0, -1, 0, 1};
	auto dfs = [&] (auto dfs, int a, int b, auto &g, auto &st) -> void {
		bool ok = false;
		for (auto &[x, y] : st) {

			for (int i = 0; i < 4; ++i) {
				int u1 = x + dx[i], v1 = y + dy[i];
				int u2 = x + 2 * dx[i], v2 = y + 2 * dy[i];
				if (u1 >= 0 && u1 < n && v1 >= 0 && v1 < m &&
					u2 >= 0 && u2 < n && v2 >= 0 && v2 < m &&
					g[u1][v1] && !g[u2][v2]) {
					ok = true;

					auto t = g;
					t[u1][v1] = false;
					t[u2][v2] = true;
					t[x][y] = false;

					// g[u1][v1] = false;
					// g[u2][v2] = true;
					// g[a][b] = false;
					auto p = st;
					p.erase({x, y});
					p.erase({u1, v1});
					p.insert({u2, v2});

					// st.erase({a, b});
					// st.erase({u1, v1});
					// st.insert({u2, v2});
					dfs(dfs, u2, v2, t, p);
				}
			}

		}
		
		if (!ok) {
			ans = min(ans, (int)st.size());
		}
		return;
	};

	set<array<int, 2>> st;
	for (int i = 0; i < k; ++i) {
		st.insert({x[i], y[i]});
	}
	for (auto &[x, y] : st) {
		dfs(dfs, x, y, g, st);
	}
	cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3392kb

input:

3
3 4 5
2 2
1 2
1 4
3 4
1 1
1 3 3
1 1
1 2
1 3
2 1 1
2 1

output:

2
3
1

result:

ok 3 number(s): "2 3 1"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3464kb

input:

20
2 1 2
1 1
2 1
5 1 3
3 1
2 1
4 1
3 3 6
1 2
2 2
1 1
2 3
3 1
3 2
4 4 4
2 3
3 1
3 2
1 2
1 1 1
1 1
5 2 6
3 2
4 1
2 1
5 2
2 2
5 1
1 3 1
1 2
1 5 1
1 5
4 6 5
4 6
4 4
2 3
4 3
1 6
6 6 3
2 4
1 3
2 1
2 2 2
2 1
1 1
5 3 4
2 2
5 1
4 3
3 2
6 5 6
5 5
6 5
2 4
2 1
3 4
1 4
2 6 5
1 6
2 1
1 4
2 3
1 3
3 5 6
2 1
3 3
1 5...

output:

2
2
3
1
1
2
1
1
3
3
2
1
3
3
2
1
3
1
2
2

result:

ok 20 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3412kb

input:

20
2 1 1
2 1
4 3 2
4 3
1 1
6 4 6
4 3
5 4
4 2
3 2
3 4
2 3
3 6 4
3 1
2 6
2 4
3 5
6 6 6
3 3
3 6
4 2
3 2
4 1
1 4
3 3 1
1 1
2 3 2
2 2
2 1
3 2 2
1 2
2 2
3 4 5
2 1
3 4
3 1
2 2
3 2
2 5 5
1 5
2 3
2 5
1 4
2 2
5 6 6
5 1
2 2
1 4
5 3
4 4
1 1
2 3 4
1 1
1 2
2 2
2 1
2 3 4
1 1
2 1
1 2
2 2
6 3 6
4 2
4 1
1 3
6 2
3 3
2...

output:

1
2
2
4
4
1
1
1
2
2
6
2
2
3
4
1
1
1
3
2

result:

ok 20 numbers

Test #4:

score: 0
Accepted
time: 0ms
memory: 3456kb

input:

20
1 5 3
1 2
1 3
1 1
5 6 6
5 4
2 4
4 4
4 6
2 5
3 3
5 2 4
4 1
3 1
4 2
5 2
6 3 6
3 1
5 2
3 2
4 1
4 3
3 3
5 3 5
5 2
2 2
3 1
4 3
3 2
6 1 4
3 1
5 1
1 1
4 1
3 5 6
1 1
2 1
3 4
1 5
2 2
3 3
2 5 2
2 5
2 1
5 4 6
1 3
4 3
2 2
1 2
3 3
2 3
2 3 3
2 3
1 1
1 3
1 1 1
1 1
1 1 1
1 1
1 1 1
1 1
6 4 5
2 1
6 2
3 2
1 2
4 2
5...

output:

2
1
2
2
1
2
3
2
2
3
1
1
1
3
2
2
2
3
2
1

result:

ok 20 numbers

Test #5:

score: 0
Accepted
time: 6ms
memory: 3472kb

input:

20
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5...

output:

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2

result:

ok 20 numbers