BFS,求助大佬,未过样例
  • 板块P1443 马的遍历
  • 楼主Valk_R
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/8/25 09:31
  • 上次更新2023/11/4 09:07:18
查看原帖
BFS,求助大佬,未过样例
69079
Valk_R楼主2021/8/25 09:31
#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
int qp[405][405];
int n,m,Ax,Ay;
bool Check(int x,int y){return x<=n&&x>0&&y>0&&y<=m;}
int a[8][2]={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};
struct node
{
	int x,y;
};
void BFS(int dx,int dy)
{
	queue <node> q;
	qp[dx][dy]=0;
	 node start,next;
	 start.x=dx;
	 start.y=dy;
	 q.push(start);
	 while(!q.empty())
	 {
			start=q.front();
			q.pop();
			for(int i=0;i<8;i++)
			{
				next.x=start.x+qp[i][0];
				next.y=start.y+qp[i][1];
				if(Check(next.x,next.y)&&qp[next.x][next.y]==-1)
				{
					qp[next.x][next.y]=qp[start.x][start.y]+1;
					q.push(next);
				//	cout<<next.x<<" "<<next.y<<" "<<qp[next.x][next.y]<<endl;
				}
			}
	 }
} 
int main()
{
	memset(qp,-1,sizeof(qp));
	cin>>n>>m>>Ax>>Ay;
	BFS(Ax,Ay);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		printf("%-5d",qp[i][j]);
		printf("\n");
	}
	return 0;
}
2021/8/25 09:31
加载中...