Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <iomanip>
- #include <iostream>
- #include <vector>
- using namespace std;
- struct Point {
- int i;
- int j;
- int dist;
- };
- void Print(const vector<vector<int>>& map) {
- for (auto row : map) {
- for (auto elem : row) {
- cout << elem << ' ';
- }
- cout << '\n';
- }
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int side_size;
- cin >> side_size;
- const int INF = side_size * side_size + 1;
- const int IS_PROCESSED = INF + 1;
- vector<vector<int>> map(side_size, vector<int>(side_size, INF));
- vector<vector<int>> result_map(side_size, vector<int>(side_size, -1));
- for (int i = 0; i < side_size; ++i) {
- for (int j = 0; j < side_size; ++j) {
- cin >> map[i][j];
- if (map[i][j] == 0) continue;
- result_map[i][j] = map[i][j];
- }
- }
- for (int i1 = 0; i1 < side_size; ++i1) {
- for (int j1 = 0; j1 < side_size; ++j1) {
- if (result_map[i1][j1] != -1) continue;
- bool is_ceil_value_found = false;
- deque<Point> d;
- vector<vector<int>> dists(side_size, vector<int>(side_size, INF));
- dists[i1][j1] = 0;
- d.emplace_back(i1, j1, dists[i1][j1]);
- while (!is_ceil_value_found) {
- int layer_size = d.size();
- for (int k = 0; k < layer_size; ++k) {
- auto [i, j, dist] = d.back();
- for (int di = -1; di <= 1; ++di) {
- for (int dj = -1; dj <= 1; ++dj) {
- if (di * di + dj * dj != 1) continue;
- int ni = i + di;
- int nj = j + dj;
- if (ni < 0 || map.size() <= ni || nj < 0 || map.front().size() <= nj) continue;
- if (dists[ni][nj] < dists[i][j] + 1) continue;
- dists[ni][nj] = dists[i][j] + 1;
- d.push_back({ ni, nj, dists[ni][nj] });
- }
- }
- }
- int cnt = 0;
- int value = 0;
- for (int i = 0; i < layer_size; ++i) {
- if (map[d.front().i][d.front().j] != 0 && map[d.front().i][d.front().j] != INF) {
- ++cnt;
- value = map[d.front().i][d.front().j];
- }
- d.pop_front();
- }
- if (cnt > 1) result_map[i1][j1] = 0;
- if (cnt == 1) {
- is_ceil_value_found = true;
- result_map[i1][j1] = value;
- Print(result_map);
- }
- }
- }
- }
- Print(result_map);
- return 0;
- }
- /*
- test1
- 3
- 0 0 0
- 1 0 2
- 0 3 0
- 1 0 2
- 1 0 2
- 0 3 0
- 2
- 0 0
- 0 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement