90pts求调
  • 板块P1443 马的遍历
  • 楼主huafan
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/6/29 17:23
  • 上次更新2025/6/30 15:03:36
查看原帖
90pts求调
1284984
huafan楼主2025/6/29 17:23
# include <bits/stdc++.h>
using namespace std;
struct node{
	int X,Y,cnt;
};
int n,m,x,y,h,t,res[405][405],nex[8][2]={{-2,-1},{-2,1},{2,1},{2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
bool flag[405][405];
node que[2000];
int main(){
	memset(res,-1,sizeof(res));
	cin>>n>>m>>x>>y;
	que[t++]=node{x,y,0};
	res[x][y]=0;
	while(h!=t){
		for(int i = 0;i < 8;i++){
			int hx=que[h].X + nex[i][0] ,hy = que[h].Y + nex[i][1] ,cnt=que[h].cnt+1;
			if(hx <= 0 || hx > n || hy <= 0 || hy > m || res[hx][hy]!=-1) continue;
			res[hx][hy] = cnt;
			que[t++] = (node){hx ,hy ,cnt};
		}h++;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cout<<res[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}
2025/6/29 17:23
加载中...