求调
查看原帖
求调
796667
ZY_jiang楼主2024/5/21 21:08
#include<bits/stdc++.h>
using namespace std;
#define ll int
const int N=205;
char a[N][N];
string s;
ll n,m,sx,sy,ex,ey;
bool vis[N][N];
int dx[]={1,1,2,2,-1,-1,-2,-2};
int dy[]={2,-2,1,-1,2,-2,1,-1};
struct node{
	ll x,y,w;
};
void bfs(ll x,ll y){
	node head,tail;
	queue <node> q;
	q.push({x,y,0});
	while(!q.empty()){
		head=q.front();
		q.pop();
		vis[head.x][head.y]=1;
		if(a[head.x][head.y]=='H'){
			cout<<head.w;
			exit(0);
		}
		for(int i=0;i<8;i++){
			tail.x=head.x+dx[i];
			tail.y=head.y+dy[i];
			tail.w=head.w+1;
			if(tail.x>=1&&tail.x<=m&&tail.y>=1&&tail.y<=n&&vis[tail.x][tail.y]==0&&a[tail.x][tail.y]!='*'){
				q.push(tail);
			}
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
			if(a[i][j]=='K'){
				sx=i;
				sy=j;
			}
			if(a[i][j]=='H'){
				ex=i;
				ey=j;
			}
		}
	}
	vis[sx][sy]=1;
	bfs(sx,sy);
//	cout<<maxn;
	return 0;
} 
2024/5/21 21:08
加载中...