30分,RE求调
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 159
char s[N][N];
bool visited[N][N];
int f[N][N],dx[]= {-2, -1, 1, 2, 2, 1, -1, -2},dy[]= {1, 2, 2, 1, -1, -2, -2, -1},qx,qy,zx,zy;int n,m;
struct node {
int x, y, v;
};
int bfs() {
queue<node> q;
q.push((node) {
qx, qy, 0
});
while (!q.empty()) {
node k = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int x = dx[i] + k.x;
int y = dy[i] + k.y;
if (x < 0 || x >= m || y < 0 || y >= n) {
continue;
}
if (f[x][y]) {
continue;
}
if (x == zx && y == zy) {
return k.v + 1;
}
f[x][y] = 1;
q.push((node) {
x, y, k.v+1
});
}
}
}
int main() {
int flag=0;
scanf("%d %d",&m,&n);
int i;
for(i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
cin>>s[i][j];
if (s[i][j] == 'K') {
qx = i, qy = j;
f[i][j] = 1;
}
if (s[i][j] == 'H') {
zx = i, zy = j;
}
if (s[i][j] == '*') {
f[i][j] = 1;
}
}
}
cout<<bfs();
}