QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#455829 | #4376. Dragon slayer | horzward# | AC ✓ | 455ms | 3636kb | C++17 | 2.2kb | 2024-06-26 20:55:37 | 2024-06-26 20:55:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
struct wall
{
int id, st, fi;
};
int n, m, k, start, finish;
queue<int> q;
wall hor[20], ver[20];
bool vis[15 * 15 + 5];
vector<int> g[15 * 15+5];
void mk(int x)
{
bool hwall[20] = {};
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
hwall[j] = 0;
for(int j=0; j<k; j++)
{
if(!((x>>j) & 1))
continue;
if(ver[j].id == -1)
continue;
if(ver[j].st <= i && i < ver[j].fi)
hwall[ver[j].id] = 1;
}
for(int j=1; j<m; j++)
{
if(hwall[j])
continue;
int one = i * m + (j-1), two = i * m + j;
g[one].push_back(two), g[two].push_back(one);
}
}
for(int j=0; j<m; j++)
{
for(int i=0; i<n; i++)
hwall[i] = 0;
for(int i=0; i<k; i++)
{
if(!((x >> i) & 1))
continue;
if(hor[i].id == -1)
continue;
if(hor[i].st <= j && j < hor[i].fi)
hwall[hor[i].id] = 1;
}
for(int i=1; i<n; i++)
{
if(hwall[i])
continue;
int one = (i-1)*m + j, two = i*m + j;
g[one].push_back(two), g[two].push_back(one);
}
}
}
bool bfs(int rm)
{
memset(vis, 0, sizeof(vis));
fill(g, g+n*m, vector<int>());
mk(rm);
q.push(start);
vis[start] = 1;
while(q.size())
{
int now = q.front();
q.pop();
for(int i: g[now])
{
if(!vis[i])
{
vis[i] = 1;
q.push(i);
}
}
}
return vis[finish];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while(t--)
{
cin >> n >> m >> k;
int w, x, y, z;
cin >> w >> x >> y >> z;
start = w * m + x, finish = y * m + z;
for(int i=0; i<k; i++)
{
cin >> w >> x >> y >> z;
if(w == y)
{
hor[i] = {w, x, z};
ver[i] = {-1, -1, -1};
}
else
{
ver[i] = {x, w, y};
hor[i] = {-1, -1, -1};
}
}
int ans = k;
for(int i=0; i<(1<<k); i++)
{
bool now = bfs(i);
if(now)
ans = min(ans, k - __builtin_popcount(i));
/*for(int j=0; j<n*m; j++)
{
cout << "(" << j/m << "," << j%m << "): ";
for(int u: g[j])
{
cout << "(" << u/m << "," << u%m << "), ";
}
cout << "\n";
}
cout << "\n";*/
}
cout << ans << "\n";
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 455ms
memory: 3636kb
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