50分 wa求调
  • 板块P1443 马的遍历
  • 楼主aqua__
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/12 18:10
  • 上次更新2024/3/12 20:25:24
查看原帖
50分 wa求调
1086963
aqua__楼主2024/3/12 18:10
#include<bits/stdc++.h>

using namespace std;

const int N = 410;

int n,m,x,y;
int a[N][N],ans[N][N];
bool vis[N * N];

void bfs(){
	memset(vis,false,sizeof vis);
	memset(ans,0,sizeof ans);
	vis[(x - 1) * m + y] = true;
	ans[x][y] = 0;
	int dx[8] = {-2,-2,2,2,1,-1,1,-1},dy[8] = {-1,1,-1,1,2,2,-2,-2};
	queue<int> q;
	q.push((x - 1) * m + y);
	while(q.size()){
		int t = q.front();
		q.pop();
		for(int i = 0;i < 8;i ++ ){
			int newx = t / m + 1 + dx[i],newy = t % m + dy[i];
			if(!vis[(newx - 1) * m + newy] && newx >= 1 && newx <= n && newy >= 1 && newy <= m) {
				ans[newx][newy] = ans[t / m + 1 ][t % m] + 1;
				vis[(newx - 1) * m + newy] = true;
				q.push((newx - 1) * m + newy);
			}
		}
	}
}

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;
		}
	}
	bfs();
	for(int i = 1;i <= n;i ++ ){
		for(int j = 1;j <= m;j ++ ){
			cout << ans[i][j] << "    ";
		}
		puts("");
	}
	return 0;
}
2024/3/12 18:10
加载中...