7个MLE QwQ
  • 板块P1443 马的遍历
  • 楼主shuhao
  • 当前回复27
  • 已保存回复28
  • 发布时间2024/8/1 10:31
  • 上次更新2024/8/1 11:22:29
查看原帖
7个MLE QwQ
1097884
shuhao楼主2024/8/1 10:31
#include <bits/stdc++.h>
using namespace std;

const int N = 400+10;
int n, m, x, y;
struct Node {
	int X, Y;
};
int a[N][N];
bool vis[N][N];
const int dx[8] = {-2,+2,-1,+1,-2,+2,-1,+1};
const int dy[8] = {-1,-1,-2,-2,+1,+1,+2,+2};
void bfs() {
	queue <Node> q;
	q.push({x, y});
	while(!q.empty()) {
		Node k = q.front(); q.pop();
		vis[k.X][k.Y] = true;
		for(int i = 0; i < 8; i++) {
			int cx = k.X+dx[i], cy = k.Y+dy[i];
			if(cx < 1 || cx > n || cy < 1 || cy > m) continue;
			if(vis[cx][cy] == true) continue;
			a[cx][cy] = a[k.X][k.Y] + 1;
			q.push({cx, cy});
		}
	}
}

int main() {
	cin >> n >> m >> x >> y;
	memset(a, -1, sizeof(a));
	a[x][y] = 0;
	bfs();
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++)
			cout << a[i][j] << ' ';
		cout << endl;
	}
	
	return 0;
}

#3~#9 全MLE

dalao救我

2024/8/1 10:31
加载中...