0pts!!!
查看原帖
0pts!!!
482253
CZKWEN543楼主2022/7/30 09:20
#include<bits/stdc++.h>
using namespace std;

int n, m, x, y, step;
queue<pair<int, int> > q;
int a[405][405];
bool vis[405][405];
const int dx[8]={-2, -1, 1, 2, 2, 1, -1, -2};
const int dy[8]={1, 2, 2, 1, -1, -2, -2, -1};

void bfs(){
	while(!q.empty()){
		int f=q.front().first;
		int s=q.front().second;
		q.pop();
		for(int i=0;i<8;i++){
			int xx=dx[i]+f;
			int yy=dy[i]+s;
			if(xx>=1 && xx<=n && yy>=1 && yy<=n && !vis[xx][yy]){
				vis[xx][yy]=1;
				a[xx][yy]=a[f][s]+1;
				q.push(make_pair(xx, yy));
			}
		}
	}
}

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;
	a[x][y]=0;
	vis[x][y]=1;
	q.push(make_pair(x, y));
	bfs();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m-1;j++) cout<<a[i][j]<<setw(5);
		cout<<a[i][m];
		cout<<"\n";
	}
	return 0;
}
2022/7/30 09:20
加载中...