题解:P7284 [COCI 2020/2021 #4] Patkice II
whisper_Luo · · 题解
题目:link
博客园:link
思路
这题怎么敢评绿啊qwq
我们考虑目前的位置是
题目转化为:每个位置
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 2e3 + 3;
const int INF = 1e9;
struct Node {
int x, y, d;
bool operator < (const Node &i) const {
return d > i.d;
}
} ;
int n, m, dist[N][N];
pii pre[N][N];
char c[N][N];
priority_queue<Node> pq;
void Push(int x, int y, int t, int k, int xx, int yy) {
if (!(x > n || x < 1 || y > m || y < 1)) {
if (dist[x][y] > k + t) {
dist[x][y] = k + t;
pre[x][y] = {xx, yy};
pq.push({x, y, k + t});
}
}
}
void Dijkstra() { // Dijkstra 或者 BFS 都可以
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dist[i][j] = INF;
if (c[i][j] == 'o') {
pq.push({i, j, -1});
dist[i][j] = -1;
}
}
}
while (!pq.empty()) {
int x = pq.top().x, y = pq.top().y, d = pq.top().d;
pq.pop();
if (dist[x][y] != d) continue ;
if (c[x][y] == 'x') return ;
int xx = c[x][y] == '^' ? -1 : c[x][y] == 'v' ? 1 : 0, yy = c[x][y] == '<' ? -1 : c[x][y] == '>' ? 1 : 0;
xx += x, yy += y;
Push(xx, yy, 0, dist[x][y], x, y), Push(x + 1, y, 1, dist[x][y], x, y), Push(x - 1, y, 1, dist[x][y], x, y), Push(x, y + 1, 1, dist[x][y], x, y), Push(x, y - 1, 1, dist[x][y], x, y);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
}
}
Dijkstra();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (c[i][j] == 'x') {
cout << dist[i][j] << "\n";
for (int k = i, l = j; c[pre[k][l].first][pre[k][l].second] != 'o'; ) {
int x = pre[k][l].first, y = pre[k][l].second;
c[x][y] = x - k == 1 ? '^' : x - k == -1 ? 'v' : y - l == 1 ? '<' : '>';
k = x, l = y;
}
for (int k = 1; k <= n; k++) {
for (int l = 1; l <= m; l++) {
cout << c[k][l];
}
cout << "\n";
}
}
}
}
return 0;
}
// 你想知道藏海花的故事吗……