Advertisement
trafik

Untitled

Nov 3rd, 2022
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned long long
  4. #define ld long double
  5. #define len(v) (int)v.size()
  6. #define all(v) v.begin(), v.end()
  7. #define rall(v) v.rbegin(), v.rend()
  8. #define pii pair<int, int>
  9. #define vi vector<int>
  10. #define vii vector<vector<int>>
  11. #define vpii vector<pair<int, int>>
  12. #define ull unsigned long long
  13. //#define int long long
  14. //#define ll ull
  15. const int N = 1e6 + 1;
  16. const int maxn = 310;
  17. const int C = 20;
  18. const int logn = 20;
  19. const int inf = 1e9;
  20. const ll mod = 1e9 + 7;
  21. const int M = 1e9;
  22. const ull M2 = 998244353;
  23. const ld eps = 1e-6;
  24. using namespace std;
  25.  
  26. // random
  27. std::mt19937_64 gen(std::chrono::steady_clock::now().time_since_epoch().count());
  28.  
  29. template<class T>
  30. istream &operator>>(istream &in, vector<T> &a) {
  31.     for (auto &i : a)
  32.         in >> i;
  33.     return in;
  34. }
  35.  
  36. template<class T>
  37. ostream &operator<<(ostream &out, vector<T> &a) {
  38.     for (auto &i : a)
  39.         out << i << ' ';
  40.     return out;
  41. }
  42.  
  43. int n, m;
  44. vector<string> a(maxn);
  45. int pr[maxn][maxn], pc[maxn][maxn];
  46. bool used[maxn][maxn][4];
  47. bool check1(int i, int j1, int j2) { // j1 ... j2
  48.     if (j2 >= m || j1 < 0) return false;
  49.     return (pr[i][j2] - (j1 > 0 ? pr[i][j1 - 1] : 0)) == 0;
  50. }
  51. bool check2(int j, int i1, int i2) { // i1 .... i2
  52.     if (i2 >= n || i1 < 0) return false;
  53.     return (pc[i2][j] - (i1 > 0 ? pc[i1 - 1][j] : 0)) == 0;
  54. }
  55. bool acc = false;
  56. void dfs(vector<int> v, int len) {
  57.     if (len == 2) {
  58.         cout << "";
  59.     }
  60.     vector<int> t;
  61.     used[v[0]][v[1]][v[2]] = true;
  62.     if (v[1] == m - 1 && v[2] == 0) {
  63.         acc = true;
  64.         return;
  65.     }
  66.     // UP OR DOWN
  67.     if (v[2] == 0) {
  68.         if (v[0] - len + 1 > 0 && check2(v[1], v[0] - len, v[0] - 1) && !used[v[0] - 1][v[1]][v[2]]) {
  69.             t = v;
  70.             t[0]--;
  71.             dfs(t, len);
  72.         }
  73.         if (v[0] + 1 < m && check2(v[1], v[0] - len + 2, v[0] + 1) && !used[v[0] + 1][v[1]][v[2]]) {
  74.             t = v;
  75.             t[0]++;
  76.             dfs(t, len);
  77.         }
  78.     }
  79.     if (v[2] == 2) {
  80.         if (v[0] > 0 && check2(v[1], v[0] - 1, v[0] + len - 2) && !used[v[0] - 1][v[1]][v[2]]) {
  81.             t = v;
  82.             t[0]--;
  83.             dfs(t, len);
  84.         }
  85.         if (v[0] + len < m && check2(v[1], v[0] + 1, v[0] + len) && !used[v[0] + 1][v[1]][v[2]]) {
  86.             t = v;
  87.             t[0]++;
  88.             dfs(t, len);
  89.         }
  90.     }
  91.     if (v[2] == 1) {
  92.         if (v[1] > 0 && check1(v[0], v[1] - 1, v[1] + len - 2) && !used[v[0]][v[1] - 1][v[2]]) {
  93.             t = v;
  94.             t[1]--;
  95.             dfs(t, len);
  96.         }
  97.         if (v[1] + len < m && check1(v[0], v[1] + 1, v[1] + len) && !used[v[0]][v[1] + 1][v[2]]) {
  98.             t = v;
  99.             t[1]++;
  100.             dfs(t, len);
  101.         }
  102.     }
  103.     if (v[2] == 3) {
  104.         if (v[1] - len + 1 > 0 && check1(v[0], v[1] - len, v[1] - 1) && !used[v[0]][v[1] - 1][v[2]]) {
  105.             t = v;
  106.             t[1]--;
  107.             dfs(t, len);
  108.         }
  109.         if (v[1] + 1 < m && check1(v[0], v[1] - len + 2, v[1] + 1) && !used[v[0]][v[1] + 1][v[2]]) {
  110.             t = v;
  111.             t[1]++;
  112.             dfs(t, len);
  113.         }
  114.     }
  115.     // TURN BY ANGLE
  116.     if (v[2] != 0) {
  117.         if (check2(v[1], v[0] - len + 1, v[0]) && !used[v[0]][v[1]][0]) {
  118.             t = v;
  119.             t[2] = 0;
  120.             dfs(t, len);
  121.         }
  122.     }
  123.     if (v[2] != 1) {
  124.         if (check1(v[0], v[1], v[1] + len - 1) && !used[v[0]][v[1]][1]) {
  125.             t = v;
  126.             t[2] = 1;
  127.             dfs(t, len);
  128.         }
  129.     }
  130.     if (v[2] != 2) {
  131.         if (check2(v[1], v[0], v[0] + len - 1) && !used[v[0]][v[1]][2]) {
  132.             t = v;
  133.             t[2] = 2;
  134.             dfs(t, len);
  135.         }
  136.     }
  137.     if (v[2] != 3) {
  138.         if (check1(v[0], v[1] - len + 1, v[1]) && !used[v[0]][v[1]][3]) {
  139.             t = v;
  140.             t[2] = 3;
  141.             dfs(t, len);
  142.         }
  143.     }
  144.     // REVERSE VERTEX
  145.     if (v[2] == 0 && !used[v[0] - len + 1][v[1]][2]) {
  146.         t = v;
  147.         t[0] = v[0] - len + 1;
  148.         t[2] = 2;
  149.         dfs(t, len);
  150.     }
  151.     if (v[2] == 1 && !used[v[0]][v[1] + len - 1][3]) {
  152.         t = v;
  153.         t[1] = v[1] + len - 1;
  154.         t[2] = 3;
  155.         dfs(t, len);
  156.     }
  157.     if (v[2] == 2 && !used[v[0] + len - 1][v[1]][0]) {
  158.         t = v;
  159.         t[0] = v[0] + len - 1;
  160.         t[2] = 0;
  161.         dfs(t, len);
  162.     }
  163.     if (v[2] == 3 && !used[v[0]][v[1] - len + 1][1]) {
  164.         t = v;
  165.         t[1] = v[1] - len + 1;
  166.         t[2] = 1;
  167.         dfs(t, len);
  168.     }
  169.  
  170. }
  171.  
  172. void solve() {
  173.     cin >> n >> m;
  174.     for (int i = 0; i < n; ++i) {
  175.         cin >> a[i];
  176.     }
  177.  
  178.     for (int i = 0; i < n; ++i) {
  179.         pr[i][0] = (a[i][0] == '#');
  180.         for (int j = 1; j < m; ++j) {
  181.             pr[i][j] = pr[i][j - 1];
  182.             if (a[i][j] == '#')
  183.                 pr[i][j]++;
  184.         }
  185.     }
  186.     for (int j = 0; j < m; ++j) {
  187.         pc[0][j] = (a[0][j] == '#');
  188.         for (int i = 1; i < n; ++i) {
  189.             pc[i][j] = pc[i - 1][j];
  190.             if (a[i][j] == '#')
  191.                 pc[i][j]++;
  192.         }
  193.     }
  194.  
  195.     int l = 0;
  196.     int r = n + 1;
  197.     while (l + 1 < r) {
  198.         for (int i = 0; i < maxn; ++i) {
  199.             for (int j = 0; j < maxn; ++j) {
  200.                 for (int k = 0; k < 4; ++k)
  201.                     used[i][j][k] = false;
  202.             }
  203.         }
  204.         int mid = (l + r) / 2;
  205.         acc = false;
  206.         vi v = {-1, 0, 0};
  207.         for (int i = mid - 1; i < n; ++i) {
  208.             if (!check2(0, i - mid + 1, i)) continue;
  209.             v[0] = i;
  210.             dfs(v, mid);
  211.         }
  212.         if (acc) l = mid;
  213.         else r = mid;
  214.     }
  215.     cout << l;
  216.  
  217. }
  218.  
  219.  
  220. signed main() {
  221.     ios::sync_with_stdio(false);
  222.     cin.tie(nullptr);
  223.     cout.tie(nullptr);
  224.  
  225.     int T = 1;
  226.     //cin >> T;
  227.     while (T--) {
  228.         solve();
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement