40分,求调
查看原帖
40分,求调
996300
CMY2013楼主2024/7/2 21:30
#include<iostream>
#include<queue>
using namespace std;
int m,n,x,y;
char a[400][400];
bool f[400][400];
int dx[8]={-2,-1,1,2,2,1,-1,-2},dy[8]={1,2,2,1,-1,-2,-2,-1};
struct node
{
	int hx,hy,c;
};
queue<node> q;
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') x=i,y=j;
		}
		
	}
	q.push({x,y,0});
	while(!q.empty())
	{
		node l=q.front();
		f[l.hx][l.hy]=true;
		if(a[l.hx][l.hy]=='H')
		{
			cout<<l.c;
			return 0;
		}
		q.pop();
		for(int i=0;i<8;i++)
		{
			
			if( l.hx+dx[i]>=1 && l.hx+dx[i]<=m && l.hy+dy[i]>=1 && l.hy+dy[i]<=n  && a[l.hx+dx[i]][l.hy+dy[i]]!='*' && !f[l.hx+dx[i]][l.hy+dy[i]]) q.push({l.hx+dx[i],l.hy+dy[i],l.c+1});
		}
	}
	return 0;
}
2024/7/2 21:30
加载中...