QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#47440 | #4376. Dragon slayer | miaomiaozi | AC ✓ | 223ms | 3752kb | C++17 | 3.0kb | 2022-09-09 19:42:19 | 2022-09-09 19:42:22 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// https://space.bilibili.com/672346917
#ifndef LOCAL
#define LOG(...) 42
#endif
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long LL;
typedef pair <int, int> PII;
constexpr int inf = 0x3f3f3f3f;
constexpr double EPS = 1e-8;
const double PI = acos(-1);
int multi_cases = 1;
const int N = 17;
struct Wall {
int x1, y1, x2, y2;
} walls[N];
void A_SOUL_AvA () {
int n, m, s;
int sx, sy, tx, ty;
cin >> n >> m >> s;
cin >> sx >> sy >> tx >> ty;
for (int i = 0; i < s; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a == c && b > d) swap(b, d);
if (b == d && a > c) swap(a, c);
walls[i] = {a, b, c, d};
}
vector vis(n + 1, vector<int>(m + 1));
vector V(n + 1, vector<int>(m + 1));
vector H(n + 1, vector<int>(m + 1));
function <bool(int, int)> dfs = [&](int x, int y) {
if (x == tx && y == ty) {
return true;
}
if (vis[x][y]) return false;
vis[x][y] = 1;
bool ok = false;
if (x - 1 >= 0 && !V[x][y] && !vis[x - 1][y]) {
ok |= dfs(x - 1, y);
}
if (x + 1 < n && !V[x + 1][y] && !vis[x + 1][y]) {
ok |= dfs(x + 1, y);
}
if (y - 1 >= 0 && !H[x][y] && !vis[x][y - 1]) {
ok |= dfs(x, y - 1);
}
if (y + 1 < m && !H[x][y + 1] && !vis[x][y + 1]) {
ok |= dfs(x, y + 1);
}
return ok;
};
auto chk = [&](int state) {
for (int i = 0; i <= n; i++) {
fill(all(vis[i]), 0);
fill(all(H[i]), 0);
fill(all(V[i]), 0);
}
for (int i = 0; i < s; i++) {
if (state >> i & 1) continue;
if (walls[i].x1 == walls[i].x2) {
// 右往左看
for (int j = walls[i].y1; j < walls[i].y2; j++)
V[walls[i].x1][j] = 1;
}
else {
// 上往下看
for (int j = walls[i].x1; j < walls[i].x2; j++)
H[j][walls[i].y1] = 1;
}
}
return dfs(sx, sy);
};
vector f(1 << s, 0);
int ans = inf;
// 0 has 1 none
for (int S = 0; S < 1 << s; S++) {
if (f[S]) continue;
bool ok = chk(S);
int cnt = __builtin_popcount(S);
if (ok) {
f[S] = 1;
ans = min(ans, cnt);
for (int i = 0; i < s; i++) {
f[S | (1 << i)] |= f[S];
}
}
}
cout << ans << endl;
}
int main () {
cin.tie(nullptr)->sync_with_stdio(false);
cout << fixed << setprecision(12);
int T = 1;
for (multi_cases && cin >> T; T; T--) {
A_SOUL_AvA ();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 223ms
memory: 3752kb
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