mr_dot_convict

183-Bit-Maps-UVa-mr.convict

Jun 10th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19.  
  20. #define debug(args...) { \
  21.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  22.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  23. }
  24. void err(istream_iterator<string> it) { it->empty();
  25.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  26. }
  27. template<typename T, typename... Args>
  28. void err(istream_iterator<string> it, T a, Args... args) {
  29.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  30.     err(++it, args...);
  31. }
  32.  
  33. const int N = 200;
  34. int mat[N][N];
  35. int n, m, idx, line_cnt;
  36. string B, D, st;
  37.  
  38. void B_to_D(int r, int c, int rr, int cc) {
  39.    if (rr < r || cc < c) return;
  40.    bool is_one = 1, is_zero = 0;
  41.    for (int i = r; i <= rr; ++i) {
  42.       for (int j = c; j <= cc; ++j){
  43.          is_one &= mat[i][j];
  44.          is_zero |= mat[i][j];
  45.       }
  46.    }
  47.    is_zero ^= 1;
  48.    if (line_cnt == 50) {
  49.       line_cnt = 0;
  50.       cout << '\n';
  51.    }
  52.    if (!is_one && !is_zero) {
  53.       cout << "D";
  54.       ++line_cnt;
  55.       int mr = (r + rr)/2, mc = (c + cc)/2;
  56.       B_to_D(r, c, mr, mc);
  57.       B_to_D(r, mc + 1, mr, cc);
  58.       B_to_D(mr + 1, c, rr, mc);
  59.       B_to_D(mr + 1, mc + 1, rr, cc);
  60.    }
  61.    else {
  62.       if (is_one) cout << "1";
  63.       else cout << "0";
  64.       ++line_cnt;
  65.    }
  66.    return;
  67. }
  68.  
  69. void D_to_B(int r, int c, int rr, int cc) {
  70.    if (rr < r || cc < c) return;
  71.    char val = B[idx];
  72.    ++idx;
  73.    if (val == 'D') {
  74.       int mr = (r + rr)/2, mc = (c + cc)/2;
  75.       D_to_B(r, c, mr, mc);
  76.       D_to_B(r, mc + 1, mr, cc);
  77.       D_to_B(mr + 1, c, rr, mc);
  78.       D_to_B(mr + 1, mc + 1, rr, cc);
  79.    }
  80.    else {
  81.       for (int i = r; i <= rr; ++i)
  82.          for (int j = c; j <= cc; ++j)
  83.             mat[i][j] = val - '0';
  84.    }
  85.    return;
  86. }
  87.  
  88. void read() {
  89.    if (st == "B") {
  90.       int cnt = n*m;
  91.       D = "";
  92.       while (cnt) {
  93.          cin >> st;
  94.          D += st;
  95.          cnt -= (int) st.size();
  96.       }
  97.       cout << "D" << setw(4) << n << setw(4) << m << '\n';
  98.  
  99.       for (int i = 0; i < n; ++i) {
  100.          for (int j = 0; j < m; ++j) {
  101.             mat[i][j] = D[m*i + j] - '0';
  102.          }
  103.       }
  104.       line_cnt = 0;
  105.       B_to_D(0, 0, n - 1, m - 1);
  106.       cout << '\n';
  107.    }
  108.    else {
  109.       cin >> B;
  110.       cout << "B" << setw(4) << n << setw(4) << m << '\n';
  111.       idx = 0;
  112.       D_to_B(0, 0, n - 1, m - 1);
  113.       line_cnt = 0;
  114.       for (int i = 0; i < n; ++i) {
  115.          for (int j = 0; j < m; ++j) {
  116.             cout << mat[i][j];
  117.             ++line_cnt;
  118.             if (!(j == m - 1 && i == n - 1) && line_cnt == 50) {
  119.                line_cnt = 0;
  120.                cout << "\n";
  121.             }
  122.          }
  123.       }
  124.       cout << '\n';
  125.    }
  126. }
  127.  
  128. signed main() {
  129.    IOS; PREC;
  130.    while (cin >> st >> n >> m, st != "#") {
  131.       read();
  132.    }
  133.    return EXIT_SUCCESS;
  134. }
Add Comment
Please, Sign In to add comment