全wa,但是下载数据是一样的
  • 板块P1443 马的遍历
  • 楼主hczyy
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/8/15 08:51
  • 上次更新2024/8/15 10:50:16
查看原帖
全wa,但是下载数据是一样的
1112330
hczyy楼主2024/8/15 08:51
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 410;

struct node{
	int x, y, step;
}q[MAXN * MAXN];

int n, m, x, y ,front = 1, vis[MAXN][MAXN] ,rear, d[8][2] = {{2,1},{-2,1},{-2,-1},{2,-1},{1,2},{1,-2},{-1,-2},{-1,2}};

void bfs(){
	memset(vis, -1, sizeof(vis));
	front = 1, rear = 1;
	q[rear].x = x, q[rear].y = y,q[rear].step = 0;
	vis[x][y] = 0;
	while(front <= rear){
		node u = q[front];
		front++;
		for(int i = 0; i < 8; i++){
			node t;
			t.x = u.x + d[i][0], t.y = u.y + d[i][1], t.step = u.step + 1;
			if(t.x >= 1 && t.y >= 1 && t.x <= n && t.y <= m && vis[t.x][t.y] == -1){
				vis[t.x][t.y] = u.step;
				q[++rear] = t;
			}
		}
	}
}

int main(){
	cin >> n >> m >> x >> y;
	bfs();
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			printf("%-5d",vis[i][j]);
		}
		cout << endl;
	}
	return 0;
}
2024/8/15 08:51
加载中...