bdf20分求大佬帮助
  • 板块P1443 马的遍历
  • 楼主PANSG
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/1/13 20:11
  • 上次更新2023/10/28 12:25:00
查看原帖
bdf20分求大佬帮助
401973
PANSG楼主2022/1/13 20:11

1号和3号测试点AC,2号和6号WA,其余TLE,求大佬帮帮忙



#include<bits/stdc++.h>
using namespace std;
const int N = 409;
int n,m,x,y;
int a[N][N];
bool check[N][N];
//队列 
int h = 0,t = 0;
int q[N][2]; 
int step = 0;

int dx[8] = {-1,1,-1,1,2,2,-2,-2};
int dy[8] = {-2,-2,2,2,-1,1,1,-1};
void bfs1(int x,int y){
	check[x][y] = true;
	for(int i=0;i<8;i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx>n || tx<1 || ty>m|| ty<1 || check[tx][ty]==true)continue;
		q[t][0] = tx;
		q[t++][1] = ty;
		check[tx][ty] = true;
	}
}

int main(){
	
	cin >> n >> m >> x >> y;
	
	for(int i = 1; i<= n;i++){
		for(int j = 1; j<=m;j++){
			a[i][j] = -1;	
			check[i][j] = false;
		}
	}
		
	q[t][0] = x;
	q[t++][1] = y; 
	int tt = t;
	a[1][1] = 0;
	while(h <= t){
		int x = q[h][0];
		int y = q[h++][1];
		
		bfs1(x,y);
		a[x][y] = step;
		
		if(h == tt){
			step++;
			tt = t;
		}
		
	}
	
	for(int i = 1; i<= n;i++){
		for(int j = 1; j<=m;j++){
			printf("%-5d",a[i][j]);
		}
		cout << endl;
	}
	return 0;
}
2022/1/13 20:11
加载中...