站外题求调
  • 板块学术版
  • 楼主AFO_Lzx
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/21 12:33
  • 上次更新2025/1/21 15:24:21
查看原帖
站外题求调
1030381
AFO_Lzx楼主2025/1/21 12:33

代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int maxn = 2e3 + 5;
int n, m, k, X, Y;
int ans = 1, st[maxn][maxn];
bool vis[maxn][maxn];

struct node {
	int x, y;
	int step;
};

queue<node> q;
const int fx[] = {0, 0, 1, 0, -1};
const int fy[] = {0, 1, 0, -1, 0};

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);

	cin >> n >> m >> k;

	for (int i = 1; i <= k; i++) {
		cin >> X >> Y;
		st[X][Y] = 1;
		vis[X][Y] = true;
		q.push({X, Y, 1});
	}

	while (q.size() > 0) {
		node u = q.front();
		q.pop();
		for (int i = 1; i <= 4; i++) {
			int tx = u.x + fx[i];
			int ty = u.y + fy[i];

			if (tx <= 0 || tx > n || ty <= 0 || ty > n || vis[tx][ty]) continue;
			st[tx][ty] = st[u.x][u.y] + 1;
			ans = st[tx][ty];
			vis[tx][ty] = true;
			q.push({tx, ty, st[tx][ty]});
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (st[i][j] == ans) {
				cout << i << " " << j << endl;
				return 0;
			}
		}
	}

	return 0;
}

求问为何 Test 1 就 WA 掉了。

2025/1/21 12:33
加载中...