#include <bits/stdc++.h>
using namespace std;
//#define int long long
int q1,q2,z1,z2;
int n,m;
struct node{
int x,y;
int step;
};
queue<node> q;
char mp[5005][5005];
bool vis[5005][5005];
int dir[8][2] = {-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2};
bool inmp(int x,int y){
return x>=1 && x<=n && y>=1 && y<=m && vis[x][y]==false && mp[x][y]=='.';
}
int ans;
void bfs(int x,int y){
q.push({x,y,0});
vis[x][y]=true;
while(!q.empty()){
node t=q.front();
q.pop();
int cx=t.x;
int cy=t.y;
int cs=t.step;
if(cx==z1 && cy==z2){
ans=cs;
return ;
}
for(int i=0;i<8;i++){
int xx=cx+dir[i][0];
int yy=cy+dir[i][1];
if(inmp(xx,yy)==true){
q.push({xx,yy,cs+1});
vis[xx][yy]=true;
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>mp[i][j];
if(mp[i][j]=='K'){
q1=i;
q2=j;
}
if(mp[i][j]=='H'){
z1=i;
z2=j;
}
}
}
bfs(q1,q2);
cout<<ans;
return 0;
}
目前的问题就是
if(cx==z1 && cy==z2){
ans=cs;
return ;
}
判断未生效