You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
#define x first
#define y second
typedef pair<int, int> PII;
/*
48
8
K:H* .:
BFS,dx8
*/
const int N = 155, M = N * N;
int n, m;
char g[N][N];
PII q[M];
int dist[N][N]; // 距离数组
// 8个方向
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
int ans = INF;
void bfs() {
// 初始化距离数组为-1
memset(dist, -1, sizeof dist);
// 出发点坐标
int sx, sy;
// 找出发点
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (g[i][j] == 'K') sx = i, sy = j;
// 队列
int hh = 0, tt = -1;
q[++tt] = {sx, sy};
dist[sx][sy] = 0; // 标识走过,防止走回头路
while (hh <= tt) {
PII t = q[hh++];
for (int i = 0; i < 8; i++) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= m) continue;
if (g[x][y] == '*') continue; // 障碍的位置用 * 来标记
if (~dist[x][y]) continue; // 已经走过不能再走,防止走回头路
if (g[x][y] == 'H') { // 草的位置用 H 来标记。
ans = dist[t.x][t.y] + 1; // 多跳一步吃到草
return; // 吃到草就OK了
}
dist[x][y] = dist[t.x][t.y] + 1; // 多跳一步
q[++tt] = {x, y}; // 入队列继续扩展
}
}
}
int main() {
cin >> m >> n; // 居然先输入列数再输入行数bt!
// 这个输入太讨厌了居然只是按行读入每次读取一行那么cin读到的就是一个字符串目标是一个字符数组的话就直接进入字符数组了~,这与以前的题目不同,要注意
for (int i = 0; i < n; i++) cin >> g[i]; // 题意下标从0开始注意~
// 找最短路
bfs();
// 输出
printf("%d\n", ans);
return 0;
}