#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std ;
const int N = 200 ;
char g[N][N] ;
int n , m ;
int fx,fy ;
int hx,hy ;
struct node{
int x , y ;
node(int xx ,int yy){
x = xx , y = yy ;
}
};
int d[8][2]= {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
queue<node> q ;
int dis[N][N] ;
void bfs(){
q.push(node(fx,fy)) ;
dis[fx][fy] = 0 ;
while(!q.empty()){
node now = q.front() ;
q.pop() ;
int x = now.x , y = now.y ;
if(x == hx && y == hy) return ;
for(int i = 0 ; i < 8 ; i ++){
int tx = x + d[i][0] , ty = y + d[i][1] ;
if(tx<0||tx>=n||ty<0||ty>=m||dis[tx][ty]!=-1||g[tx][ty] == '*') continue ;
q.push(node(tx,ty)) ;
dis[tx][ty] = dis[x][y] + 1 ;
}
}
}
int main(){
cin >> m >> n ;
memset(dis,-1,sizeof(dis)) ;
for(int i = 0 ; i < n ;i ++){
for(int j = 0 ; j < m ;j ++){
cin >> g[i][j] ;
if(g[i][j] == 'K') fx = i , fy = j ;
if(g[i][j] == 'H') hx = i , hy = j ;
}
}
bfs() ;
cout << dis[hx][hy] << endl ;
}
06-10
361
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交