第 39 个点是 n=900,m=900,d=5
样例看不全,输出 -1,但答案是有结果的QAQ。
#include <bits/stdc++.h>
using namespace std;
const int N = 1005, M = 5005;
int n, m, d, id[N][N], cnt;
int to[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char c[N][N];
bool vis[N][N], can[M][M];
struct node {int x, y, d;};
vector<node> R, Pos;
vector<int> hav[M], ans, tmp;
queue<node> q;
void bfs(int sx, int sy) {
while (!q.empty()) q.pop();
memset(vis, 0, sizeof(vis));
q.push(node {sx, sy, 0}); vis[sx][sy] = 1;
while (!q.empty()) {
int x = q.front().x, y = q.front().y, d0 = q.front().d;
q.pop();
if (d0 == d) return;
for (int i = 0; i < 4; i++) {
int x0 = x + to[i][0], y0 = y + to[i][1];
if (x0<1||x0>n||y0<1||y0>m) continue;
if (vis[x0][y0]||c[x0][y0]=='X') continue;
vis[x0][y0] = 1;
q.push(node {x0, y0, d0 + 1});
}
}
}
bool check(int x) {
for (int i : ans)
if (i == x) return 1;
return 0;
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
scanf("%d%d%d", &n, &m, &d);
for (int i = 1; i <= n; i++) {
string s; cin >> s;
for (int j = 1; j <= m; j++) {
c[i][j] = s[j - 1];
if (c[i][j] == 'R')
R.push_back({i, j, 0});
}
}
if (R.size() > 4 * d * d + 4 * d + 2) {
printf("-1\n"); return 0;
}
Pos.push_back({0, 0, 0});
for (int x = 0; x < R.size(); x++) {
bfs(R[x].x, R[x].y);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (vis[i][j]) {
if (!id[i][j])
id[i][j] = ++cnt, Pos.push_back({i, j, 0});
can[x][id[i][j]] = 1, hav[x].push_back(id[i][j]);
}
}
for (int i = 1; i < Pos.size(); i++) {
ans.clear();
for (int j = 1; j < Pos.size(); j++)
if (i != j) ans.push_back(j);
for (int j = 0; j < R.size(); j++)
if (!can[j][i]) {
tmp.clear();
for (int now : hav[j])
if (check(now)) tmp.push_back(now);
ans.clear();
for (int now : tmp) ans.push_back(now);
if (!ans.size()) break;
}
if (ans.size()) {
printf("%d %d %d %d\n", Pos[i].x, Pos[i].y, Pos[ans[0]].x, Pos[ans[0]].y);
return 0;
}
}
printf("-1\n");
return 0;
}