目录
- 答疑
P4011 孤岛营救
- @ 2026-1-2 20:22:48
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m, p, k, s;
int g[12][12][12][12];
int ky[12][12];
int d[12][12][1025];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct st {
int x, y, s;
};
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> p >> k;
for (int i = 0; i < k; ++i) {
int x1, y1, x2, y2, t;
cin >> x1 >> y1 >> x2 >> y2 >> t;
if (t == 0) t = -1;
g[x1][y1][x2][y2] = t;
g[x2][y2][x1][y1] = t;
}
cin >> s;
for (int i = 0; i < s; ++i) {
int x, y, z;
cin >> x >> y >> z;
ky[x][y] |= (1 << (z - 1));
}
memset(d, -1, sizeof(d));
queue<st> q;
int ms = ky[1][1];
d[1][1][ms] = 0;
q.push({1, 1, ms});
while (!q.empty()) {
st u = q.front(); q.pop();
if (u.x == n && u.y == m) {
cout << d[u.x][u.y][u.s] << "\n";
return 0;
}
for (int i = 0; i < 4; ++i) {
int nx = u.x + dx[i];
int ny = u.y + dy[i];
if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
int t = g[u.x][u.y][nx][ny];
if (t == -1) continue;
if (t > 0 && !((u.s >> (t - 1)) & 1)) continue;
int ns = u.s | ky[nx][ny];
if (d[nx][ny][ns] == -1) {
d[nx][ny][ns] = d[u.x][u.y][u.s] + 1;
q.push({nx, ny, ns});
}
}
}
cout << -1 << "\n";
return 0;
}
0 条评论
目前还没有评论...
京公网安备11010802045784号
YIZHIYANG 一只羊 LV 8