样例没过,求助,可关
  • 板块P1443 马的遍历
  • 楼主xcjyz
  • 当前回复2
  • 已保存回复2
  • 发布时间2025/1/12 17:24
  • 上次更新2025/1/12 21:41:47
查看原帖
样例没过,求助,可关
1188445
xcjyz楼主2025/1/12 17:24
#include<bits/stdc++.h>
using namespace std;
struct bl{
	int x,y,s;
}qd,xd;
queue<bl> q;
int i,j,n,m,x,y,b[406][406],fx[8][2]={-2,1,-1,2,2,-1,-1,2,-2,-1,-1,-2,1,-2,-2,1};
int pd(int x,int y){
	return b[x][y]==-1&&x>0&&x<=n&&y>0&&y<=m;
}
void bfs(){
	while(!q.empty()){
		qd=q.front();
		q.pop();
		for(int i=0;i<8;i++){
			xd.x=qd.x+fx[i][0];
			xd.y=qd.y+fx[i][1];
			xd.s=qd.s+1;
			if(pd(xd.x,xd.y)){
				b[xd.x][xd.y]=xd.s;
				q.push(xd);
			}
		}
		
	}
}
int main()
{
	cin>>n>>m>>x>>y;
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b[i][j]=-1;
	b[x][y]=0;
	q.push({x,y,0});
	bfs();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) cout<<b[i][j]<<" ";
		cout<<endl;
	}
	return 0;
}

2025/1/12 17:24
加载中...