Advertisement
Guest User

Palindromic Crossword

a guest
Aug 22nd, 2021
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define all(x) (x).begin(),(x).end()
  5. #define pb push_back
  6. #define pf push_front
  7. #define sz(x) (int)(x).size()
  8. #define lb lower_bound
  9. #define ub upper_bound
  10. #define mp make_pair
  11. #define fi first
  12. #define se second
  13. #define setbits(x) __builtin_popcount(x)
  14. #define zerobits(x) __builtin_ctz(x)
  15. #define setbitsll(x) __builtin_popcountll(x)
  16. #define zerobitsll(x) __builtin_ctzll(x)
  17. #define ps(x,y) fixed<<setprecision(y)<<x
  18.  
  19. typedef vector<int> vi;
  20. typedef long long ll;
  21. typedef vector<ll> vl;
  22. typedef pair<int,int> pii;
  23. typedef unsigned long long ull;
  24. typedef long double ld;
  25. typedef map<int,int> mii;
  26.  
  27. const int MOD = 1e9+7;
  28. const ld PI = acos((ld)-1);
  29. const int maxN = 1005;
  30.  
  31. string grid[maxN],res[maxN];
  32. int row1[maxN][maxN],col1[maxN][maxN],row2[maxN][maxN],col2[maxN][maxN],n,m;
  33. bool visited[maxN][maxN];
  34.  
  35. void dfs(int r,int c,char ch) {
  36.     if (r < 0 || c < 0 || r >= n || c >= m) return;
  37.     res[r][c] = ch;
  38.     visited[r][c] = true;
  39.     int u = col1[r][c]+1;
  40.     int d = col2[r][c]-1;
  41.     int mirrorVertial = d-r+u;
  42.     if (mirrorVertial >= 0 && mirrorVertial < n && !visited[mirrorVertial][c] && grid[mirrorVertial][c] != '#') {
  43.         dfs(mirrorVertial,c,ch);
  44.     }
  45.     int left = row1[r][c]+1;
  46.     int right = row2[r][c]-1;
  47.     int mirrorHorizontal = right-c+left;
  48.     if (mirrorHorizontal >= 0 && mirrorHorizontal < m && !visited[r][mirrorHorizontal] && grid[r][mirrorHorizontal] != '#') {
  49.         dfs(r,mirrorHorizontal,ch);
  50.     }
  51.  
  52. }
  53.  
  54. void solve () {
  55.     cin >> n >> m;
  56.     for (int i = 0;i < n;++i) {
  57.         cin >> grid[i];
  58.     }
  59.     for (int i = 0;i < n;++i) {
  60.         for (int j = 0;j < m;++j) {
  61.             res[i][j] = grid[i][j];
  62.             visited[i][j] = false;
  63.             row1[i][j] = col1[i][j] = -1;
  64.             row2[i][j] = m;
  65.             col2[i][j] = n;
  66.         }
  67.     }
  68.     for (int i = 0;i < n;++i) {
  69.         for (int j = 0;j < m;++j) {
  70.             if (grid[i][j] == '#') {
  71.                 row1[i][j] = j;
  72.             } else {
  73.                 if (j) {
  74.                     row1[i][j] = row1[i][j-1];
  75.                 }
  76.             }
  77.         }
  78.         for (int j = m-1;j >= 0;--j) {
  79.             if (grid[i][j] == '#') {
  80.                 row2[i][j] = j;
  81.             } else {
  82.                 if (j < m-1) {
  83.                     row2[i][j] = row2[i][j+1];
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     for (int j = 0;j < m;++j) {
  89.         for (int i = 0;i < n;++i) {
  90.             if (grid[i][j] == '#') {
  91.                 col1[i][j] = i;
  92.             } else {
  93.                 if (i) {
  94.                     col1[i][j] = col1[i-1][j];
  95.                 }
  96.             }
  97.         }
  98.         for (int i = n-1;i >= 0;--i) {
  99.             if (grid[i][j] == '#') {
  100.                 col2[i][j] = i;
  101.             } else {
  102.                 if (i < n-1) {
  103.                     col2[i][j] = col2[i+1][j];
  104.                 }
  105.             }
  106.         }
  107.  
  108.     }
  109.     for (int i = 0;i < n;++i) {
  110.         for (int j = 0;j < m;++j) {
  111.             if (grid[i][j] != '.' && grid[i][j] != '#' && !visited[i][j]) {
  112.                 dfs(i,j,grid[i][j]);
  113.             }
  114.         }
  115.     }
  116.     int cnt = 0;
  117.     for (int i = 0;i < n;++i) {
  118.         for (int j = 0;j < m;++j) {
  119.             if (grid[i][j] == '.' && res[i][j] != '.') ++cnt;
  120.         }
  121.     }
  122.     cout << cnt << '\n';
  123.     for (int i = 0;i < n;++i) {
  124.         for (int j = 0;j < m;++j) {
  125.             cout << res[i][j];
  126.         }
  127.         cout << '\n';
  128.     }
  129. }
  130.  
  131. int main () {
  132.  
  133. #ifndef ONLINE_JUDGE
  134.  freopen("input1.txt","r",stdin);
  135.  freopen("output1.txt","w",stdout);
  136. #endif
  137.  
  138.     ios_base::sync_with_stdio(false);
  139.     cin.tie(NULL);
  140.     int t;
  141.     cin >> t;
  142.     for (int tc = 1;tc <= t;++tc) {
  143.         cout << "Case #" << tc << ": ";
  144.         solve();
  145.     }
  146.     return 0;
  147. }
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement