30分,7MLE,求救
查看原帖
30分,7MLE,求救
1068496
zhou_yihe楼主2024/8/19 14:31

代码:

#include<bits/stdc++.h>
using namespace std;


struct node{
	int x;
	int y;
	int step;
};

int a[401][401];
int dx[8]={2,2,-2,-2,1,1,-1,-1};
int dy[8]={1,-1,1,-1,2,-2,2,-2};
int n,m;

bool check(int x,int y){
	return x>=1 && x<=n && y>=1 && y<=m && a[x][y]==-1;
}

void bfs(int sx,int sy){
	queue<node> q;
	q.push({sx,sy,0});
	while(!q.empty()){
		node tmp=q.front();
		q.pop();
		a[tmp.x][tmp.y]=tmp.step;
		for(int i=0;i<8;i++){
			int nx=tmp.x+dx[i];
			int ny=tmp.y+dy[i];
			if(check(nx,ny)){
				q.push({nx,ny,tmp.step+1});
			}
		}
	}
}

int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int sx,sy;
	cin>>n>>m>>sx>>sy;
	memset(a,-1,sizeof(a));
	
	bfs(sx,sy);
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			printf("%-5d",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

2024/8/19 14:31
加载中...