Advertisement
dmkozyrev

621.cpp

May 12th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. struct Record {
  6.     int dist, count, row, col;
  7.    
  8.     Record(int dist = 100000, int count = 0, int row = -1, int col = -1)
  9.         : dist(dist)
  10.         , count(count)
  11.         , row(row)
  12.         , col(col)
  13.     { }
  14.    
  15.     Record& inc() {
  16.         return ++dist, *this;
  17.     }
  18. };
  19.  
  20. Record combine(const Record& a, const Record& b) {
  21.     if (a.row == b.row && a.col == b.col) {
  22.         return a;
  23.     } else if (a.dist > b.dist) {
  24.         return b;
  25.     } else if (a.dist < b.dist) {
  26.         return a;
  27.     } else {
  28.         return Record(a.dist, a.count+b.count, -1, -1);
  29.     }
  30. }
  31.  
  32. int main() {
  33.     freopen("input.txt", "rt", stdin);
  34.     int n; scanf("%d", &n);
  35.     std::vector<std::vector<int>> arr(n, std::vector<int>(n));
  36.     for (int i = 0; i < n; ++i) {
  37.         for (int j = 0; j < n; ++j) {
  38.             scanf("%d", &arr[i][j]);
  39.         }
  40.     }
  41.    
  42.     std::vector<std::vector<Record>> best_l_u(n, std::vector<Record>(n));
  43.     auto best_l_d = best_l_u;
  44.     auto best_r_u = best_l_d;
  45.     auto best_r_d = best_r_u;
  46.     for (int i = 0; i < n; ++i) {
  47.         for (int j = 0; j < n; ++j) {
  48.             if (arr[i][j] != 0) {
  49.                 best_l_u[i][j] = Record(0, 1, i, j);
  50.             } else if (i != 0 && j != 0) {
  51.                 best_l_u[i][j] = combine(best_l_u[i-1][j], best_l_u[i][j-1]).inc();
  52.             } else if (i != 0) {
  53.                 best_l_u[i][j] = best_l_u[i-1][j].inc();
  54.             } else if (j != 0) {
  55.                 best_l_u[i][j] = best_l_u[i][j-1].inc();
  56.             }
  57.         }
  58.         for (int j = n-1; j >= 0; --j) {
  59.             if (arr[i][j] != 0) {
  60.                 best_r_u[i][j] = Record(0, 1, i, j);
  61.             } else if (i != 0 && j != n-1) {
  62.                 best_r_u[i][j] = combine(best_r_u[i][j+1], best_r_u[i-1][j]).inc();
  63.             } else if (i != 0) {
  64.                 best_r_u[i][j] = best_r_u[i-1][j].inc();
  65.             } else if (j != n-1) {
  66.                 best_r_u[i][j] = best_r_u[i][j+1].inc();
  67.             }
  68.         }
  69.     }
  70.     for (int i = n-1; i >= 0; --i) {
  71.         for (int j = 0; j < n; ++j) {
  72.             if (arr[i][j] != 0) {
  73.                 best_l_d[i][j] = Record(0, 1, i, j);
  74.             } else if (i != n-1 && j != 0) {
  75.                 best_l_d[i][j] = combine(best_l_d[i+1][j], best_l_d[i][j-1]).inc();
  76.             } else if (i != n-1) {
  77.                 best_l_d[i][j] = best_l_d[i+1][j].inc();
  78.             } else if (j != 0) {
  79.                 best_l_d[i][j] = best_l_d[i][j-1].inc();
  80.             }
  81.         }
  82.         for (int j = n-1; j >= 0; --j) {
  83.             if (arr[i][j] != 0) {
  84.                 best_r_d[i][j] = Record(0, 1, i, j);
  85.             } else if (i != n-1 && j != n-1) {
  86.                 best_r_d[i][j] = combine(best_r_d[i][j+1], best_r_d[i+1][j]).inc();
  87.             } else if (i != n-1) {
  88.                 best_r_d[i][j] = best_r_d[i+1][j].inc();
  89.             } else if (j != n-1) {
  90.                 best_r_d[i][j] = best_r_d[i][j+1].inc();
  91.             }
  92.         }
  93.     }
  94.    
  95.     for (int i = 0; i < n; ++i) {
  96.         for (int j = 0; j < n; ++j) {
  97.             if (arr[i][j] != 0) continue;
  98.             auto best = combine(
  99.                 combine(best_l_u[i][j], best_r_u[i][j]),
  100.                 combine(best_l_d[i][j], best_r_d[i][j])
  101.             );
  102.             if (best.count == 1) {
  103.                 arr[i][j] = arr[best.row][best.col];
  104.             }
  105.         }
  106.     }
  107.     for (auto& row : arr) {
  108.         for (auto& it : row) printf("%d ", it);
  109.         printf("\n");
  110.     }
  111.    
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement