188 武士风度的牛
作者:
jy9
,
2024-08-06 13:13:14
,
所有人可见
,
阅读 1
#include <iostream>
#include <queue>
using namespace std;
const int N = 160;
const int INF = 0x3f3f3f3f;
struct node{
int x, y;
};
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int c, r;
char mp[N][N];
int dist[N][N];
int startx, starty;
int st[N][N];
void bfs(){
queue<node> q;
q.push({startx, starty});
dist[startx][starty] = 0;
while (q.size()){
node t = q.front();
q.pop();
for (int i = 0; i < 8; i ++ ){
int xx = t.x + dx[i];
int yy = t.y + dy[i];
if(xx <= 0 || xx > r || yy <= 0 || yy > c || mp[xx][yy] == '*' || st[xx][yy]) continue;
st[xx][yy] = true;
dist[xx][yy] = dist[t.x][t.y] + 1;
if (mp[xx][yy] == 'H'){
cout << dist[xx][yy];
return;
}
q.push({xx, yy});
}
}
}
int main(){
cin >> c >> r;
for (int i = 1; i <= r; i ++ ){
for (int j = 1; j <= c; j ++ ){
cin >> mp[i][j];
if(mp[i][j] == 'K'){
startx = i;
starty = j;
}
}
}
bfs();
return 0;
}