#5 #& #10 WA,hellp
查看原帖
#5 #& #10 WA,hellp
1046423
wwwee楼主2024/6/16 13:07
#include<bits/stdc++.h>
using namespace std;
long long n,i,j,ans=0,m,p=0,s[1005][1005],t=0,Lx,Ly;;
char a[100][100];
int qx[9]={0,-2,-2,-1,-1,1,1,2,2};
int qy[9]={0,1,-1,-2,2,-2,2,1,-1};    
struct node
{
	int x,y,step;
};
queue<node> q;
void bfs(int cx,int cy)
{
	q.push({cx,cy,0});
	while(!q.empty())
	{
		int x=q.front().x;
		int y=q.front().y;
		int step=q.front().step;
		q.pop();
		for(int i=1;i<=8;i++)
		{   
			int xx=x+qx[i];
			int yy=y+qy[i];
			if(xx>=1&&xx<=m&&yy>=1&&yy<=n&&a[xx][yy]=='.')
			{
				a[xx][yy]=char(step+1+'0');
				q.push({xx,yy,step+1});
			}
			if(a[xx][yy]=='H')
			{
				cout<<step+1;
				return;
			}
		}
	}
}			
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(i=1;i<=m;i++)
		for(j=1;j<=n;j++)
		{
					cin>>a[i][j];
			if(a[i][j]=='K')Lx=i,Ly=j;
		}
	bfs(Lx,Ly);
	
	return 0;
}
2024/6/16 13:07
加载中...