BFS,样例过不了,蒟蒻求调
  • 板块P1443 马的遍历
  • 楼主isop
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/27 20:46
  • 上次更新2024/7/27 20:53:05
查看原帖
BFS,样例过不了,蒟蒻求调
1071516
isop楼主2024/7/27 20:46
#include<bits/stdc++.h>
using namespace std;

int n,m,x,y;
int dx[8]={2,2,1,1,-2,-2,-1,-1};
int dy[8]={1,-1,2,-2,1,-1,2,-2};
int out[405][405];//输出 
bool vis[405][405];
struct node
{
	int x,y;
};
queue<node>q;//坐标 
queue<int>p;//步数 

inline void init()
{
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) out[i][j]=-1;
}

inline void bfs()
{
	int step=0;
	while(!q.empty())
	{
		node now=q.front();
		step=p.front();
		out[x][y]=step;
		q.pop(),p.pop();
		for(int i=0;i<8;i++)
		{
			int a=now.x+dx[i],b=now.y+dy[i];
			if(a<1 || a>n || b<1 || b>m || vis[a][b]) continue;
			vis[a][b]=true;
			q.push({a,b});
			p.push(step+1);
		}
	}
}

int main()
{
	cin>>n>>m>>x>>y;
	init();//初始化 
	q.push({x,y});
	p.push(0);
	vis[x][y]=1;
	bfs();
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
			cout<<out[i][j]<<"    ";
		cout<<endl;
	}
}
2024/7/27 20:46
加载中...