蒟蒻求助
查看原帖
蒟蒻求助
567002
Zhangky2021楼主2022/10/26 20:26
#include<iostream>
#include<queue>
using namespace std;
int n, m, o, k, a[100][100], vis[100][100];
struct pos {
	int x, y, cost;
	pos (int ax, int ay, int acost){
		x=ax, y=ay, cost=acost;
	}
};
void bfs(int x, int y, int cost){
	queue <pos> q;
	q.push(pos(x, y, cost));
	while(!q.empty()){
		pos now=q.front();
		q.pop();
		int x=now.x, y=now.y, cost=now.cost;
		if(x<1||x>n||y<1||y>m||vis[x][y])
		continue;
		a[x][y]=cost;
		vis[x][y]=1;
		q.push(pos(x+2, y+1, cost+1));
		q.push(pos(x-2, y+1, cost+1));
		q.push(pos(x+2, y-1, cost+1));
		q.push(pos(x-2, y-1, cost+1));
		q.push(pos(x+1, y+2, cost+1));
		q.push(pos(x-1, y+2, cost+1));
		q.push(pos(x+1, y-2, cost+1));
		q.push(pos(x-1, y-2, cost+1));
	}
}
int main(){
	cin>>n>>m>>o>>k;;
	bfs(o, k, 0);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(a[i][j]==0){
				if(i!=o||j!=k)
				cout<<"-1"<<" ";
				else cout<<0<<" ";
			}
			else{
				cout<<a[i][j]<<" ";
			}
		}
		cout<<endl;
	}
	return 0;
}

0pts

2022/10/26 20:26
加载中...