好吧我没读清楚题目,和象棋中的马还是有点不一样的,不会卡脚
#include<cstdio>
#include<queue>
using namespace std;
const int N = 155;
char a[N][N], b[N][N];
int dir[8][2] = {-1, 2, -1, -2, 1, -2, 1, 2, -2, -1, -2, 1, 2, -1, 2, 1};
int rock[8][2] = {0, 1, 0, -1, 0, -1, 0, 1, -1, 0, -1, 0, 1, 0, 1, 0};
int vis[N][N];
int n, m;
int g(int x, int y){
return x >= 0 && x < n && y >= 0 && y < m;
}
int main(){
scanf("%d%d", &m, &n);
for(int i = 0; i < n; ++i){
scanf("%s", a[i]);
}
int fx = -1, fy = -1;
int hx = -1, hy = -1;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(a[i][j] == 'K'){
fx = i, fy = j;
}
if(a[i][j] == 'H'){
hx = i, hy = j;
}
}
}
//printf("%d %d %d %d\n", fx, fy, hx, hy);
queue<pair<int, int> > q;
q.push({fx, fy});
while(q.size()){
int x = q.front().first;
int y = q.front().second;
q.pop();
//printf("%d %d\n", x, y);
if(x == hx && y == hy) break;
for(int i = 0; i < 8; ++i){
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(g(xx, yy) && vis[xx][yy] == 0 && a[xx][yy] != '*'){
vis[xx][yy] = 1;
b[xx][yy] = b[x][y] + 1;
q.push({xx, yy});
}
}
}
printf("%d", b[hx][hy]);
return 0;
}