QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#47489#4376. Dragon slayerneko_nyaa#AC ✓747ms3520kbC++1.9kb2022-09-10 11:20:332022-09-10 11:20:35

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-10 11:20:35]
  • 评测
  • 测评结果:AC
  • 用时:747ms
  • 内存:3520kb
  • [2022-09-10 11:20:33]
  • 提交

answer

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

#define int long long

bool vis[15][15];
vector<int> dx = {-1, 1, 0, 0};
vector<int> dy = {0, 0, -1, 1};

bool ok(int n, int m, int xs, int ys, int xt, int yt, vector<tuple<int, int, int, int>> wall) {
	memset(vis, 0, sizeof(vis));

	queue<pair<int, int>> q;
	vis[xs][ys] = 1;
	q.push({xs, ys});

	while (q.size()) {
		auto [x, y] = q.front(); q.pop();

		for (int d = 0; d < 4; d++) {
			int nx = x + dx[d];
			int ny = y + dy[d];

			if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
			if (vis[nx][ny]) continue;
			bool can = 1;
			for (auto [x1, y1, x2, y2]: wall) {
				if (x != nx) {
					if (y1 == y2) continue;

					bool inside = 0;
					if (y >= min(y1, y2) && y < max(y1, y2)) inside = 1;
					bool side1 = (x >= x1);
					bool side2 = (nx >= x1);

					if (inside && side1 != side2) can = 0;
				}
				if (y != ny) {
					if (x1 == x2) continue;

					bool inside = 0;
					if (x >= min(x1, x2) && x < max(x1, x2)) inside = 1;
					bool side1 = (y >= y1);
					bool side2 = (ny >= y1);

					if (inside && side1 != side2) can = 0;
				}
			}
			if (can) {
				vis[nx][ny] = 1;
				q.push({nx, ny});
			}
		}
	}
	return vis[xt][yt];
}

void solve() {
	int n, m, k; cin >> n >> m >> k;
	int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
	vector<tuple<int, int, int, int>> walls;
	for (int i = 0; i < k; i++) {
		int a, b, c, d; cin >> a >> b >> c >> d;
		walls.emplace_back(a, b, c, d);
	}

	int ans = k;
	for (int i = 0; i < (1 << k); i++) {
		vector<tuple<int, int, int, int>> wall;
		for (int j = 0; j < k; j++) {
			if (i & (1 << j)) {
				wall.push_back(walls[j]);
			}
		}

		if (ok(n, m, x1, y1, x2, y2, wall))  {
			ans = min(ans, (int)walls.size() - (int)wall.size());
		}
	}
	cout << ans << '\n';
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0); 
	
	int t; cin >> t;
	while (t--) {
		solve();
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 747ms
memory: 3520kb

input:

10
4 4 4 0 2 3 3
0 2 4 2
1 3 1 0
2 4 2 1
3 1 3 4
3 2 2 0 0 2 1
0 1 3 1
1 0 1 2
3 2 2 0 0 2 1
2 1 2 2
1 0 1 1
15 15 15 3 12 4 1
8 0 8 15
1 11 15 11
1 1 1 15
3 1 3 15
0 10 14 10
14 1 14 14
8 1 8 15
1 5 14 5
0 15 14 15
0 4 14 4
0 2 15 2
11 0 11 15
4 1 4 15
1 11 15 11
1 12 14 12
15 15 15 8 5 14 0
0 12 1...

output:

1
2
0
5
3
5
1
4
1
0

result:

ok 10 lines