求助求助,BFS20分,样例通过
查看原帖
求助求助,BFS20分,样例通过
579727
Plum_Steven楼主2023/1/8 22:16
#include <iostream>
#include <queue> 
using namespace std;

struct Node{
	int x;
	int y;
	int step;
};

int board[401][401]={0};
int dx[] = {-1, 1, -2, -2, -1, 1, 2, 2};
int dy[] = {2, 2, 1, -1, -2, -2, 1, -1};
bool vis[401][401]={0};

int main()
{
	queue<Node> nodes;
	int n, m, x, y;
	scanf("%d%d%d%d", &n, &m, &x, &y);
	int xx=x, yy=y;
	vis[x][y] = 1;
	board[x][y] = -1;
	for(int i=0;i<8;i++){
		if(xx+dx[i]>0&&xx+dx[i]<=m&&yy+dy[i]>0&&yy+dy[i]<=n){
			nodes.push((Node){xx+dx[i], yy+dy[i], 1});
			vis[xx+dx[i]][yy+dy[i]] = 1;
			board[xx+dx[i]][yy+dy[i]] = 1;
		}
	}
	while(!nodes.empty()){
		Node temp = nodes.front();
		nodes.pop();
		xx = temp.x;
		yy = temp.y;
		for(int i=0;i<8;i++){
			if(xx+dx[i]>0&&xx+dx[i]<=m&&yy+dy[i]>0&&yy+dy[i]<=n&&!vis[xx+dx[i]][yy+dy[i]]){
				nodes.push((Node){xx+dx[i], yy+dy[i], temp.step+1});
				vis[xx+dx[i]][yy+dy[i]] = 1;
				board[xx+dx[i]][yy+dy[i]] = temp.step+1;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(board[i][j]==-1){
				board[i][j] = 0;
			} else if(board[i][j]==0){
				board[i][j] = -1;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) printf("%d ", board[i][j]);
		printf("\n");
	}
	return 0;
}

有大佬可以帮帮本蒟蒻吗QwQ

2023/1/8 22:16
加载中...