1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <iostream> #include <vector> #include <set> #include <algorithm> #include <tuple> #include <queue> #include <map> #define maxn 200010 #define ll long long #define INF 1000000000 using namespace std;
int dx[] = { 0, 0, 1, -1 }; int dy[] = { 1, -1, 0, 0 };
int n, a[maxn], b[maxn];
set<pair<int, int>> S, T; map<pair<int, int>, int> id;
set<pair<int, int>> vis; pair<int, int> ans[maxn]; void bfs() { queue<tuple<int, int, int, int>> Q; for (auto [x, y] : T) Q.push({ x, y, x, y }), vis.insert({ x, y }); while (!Q.empty()) { int i, j, sx, sy; tie(i, j, sx, sy) = Q.front(); Q.pop(); if (S.count({ i, j })) ans[id[{ i, j }]] = make_pair(sx, sy); for (int k = 0; k < 4; ++k) { int x = i + dx[k], y = j + dy[k]; if (vis.count({ x, y }) || !S.count({ x, y }) && !T.count({ x, y })) continue; vis.insert({ x, y }); Q.push({ x, y, sx, sy }); } } }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i] >> b[i]; S.insert({ a[i], b[i] }); id[{ a[i], b[i] }] = i; } for (int i = 1; i <= n; ++i) for (int k = 0; k < 4; ++k) { int x = a[i] + dx[k], y = b[i] + dy[k]; if (S.count({ x, y })) continue; T.insert({ x, y }); } bfs(); for (int i = 1; i <= n; ++i) cout << ans[i].first << " " << ans[i].second << "\n"; return 0; }
|