条
  • 板块学术版
  • 楼主Nostopathy
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/6/22 17:33
  • 上次更新2024/6/22 20:44:29
查看原帖
条
1286053
Nostopathy楼主2024/6/22 17:33

https://www.luogu.com.cn/problem/P1443

#include <bits/stdc++.h>
using namespace std;
struct node {int first, second;} ;
const int x_[8]={-1, -2, -2, -1, 1, 2, 2, 1}, y_[8]={2, 1, -1, -2, 2, 1, -1, -2};
queue <node> q;
int n, m, x, y, stp[502][502], v[502][502];
void BFS(){
	stp[x][y]=0, v[x][y]=1;
	q.push({x, y});
	while(q.size()){
		int nx=q.front().first, ny=q.front().second;
		for(int i=0; i<8; ++i){
			int tx=nx+x_[i], ty=ny+y_[i];
			if(tx>0 && tx<n+1 && ty>0 && ty<m+1 && v[tx][ty]==0){
				v[tx][ty]++;
				q.push({tx, ty});
				stp[tx][ty]=stp[nx][ny]+1;
			}
		}
	}
}
int main(){
	cin >> n >> m >> x >> y;
	memset(stp, -1, sizeof stp);
	BFS();
	for(int i=1; i<n+1; ++i){for(int j=1; j<m+1; ++j) cout<<setw(5)<<stp[i][j]; cout<<'\n';}
}
2024/6/22 17:33
加载中...