Advertisement
_rashed

SRM297-D1-500 CageTheMonster

Oct 10th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7.  
  8. class CageTheMonster{
  9.     public:
  10.     char graph[40][40];
  11.     char graph_orig[40][40];
  12.     vector<pair<int,int>> p_start;
  13.     int n,m;
  14.     queue<pair<int,int>> q;
  15.     map<pair<int,int>,int> vis;
  16.     int dx[4] = {1,-1,0,0};
  17.     int dy[4] = {0,0,-1,1};
  18.  
  19.     int update(int x, pair<int,int> start) {
  20.         int ret = 0;
  21.         if(x & 1) {
  22.             if(start.first < n-1) {
  23.                 ret++;
  24.                 for(int i = 0; i < m; i++) {
  25.                     graph[start.first+1][i] = '#';
  26.                 }
  27.             }
  28.         }
  29.         if(x & 2) {
  30.             if(start.first > 0) {
  31.                 ret++;
  32.                 for(int i = 0; i < m; i++) {
  33.                     graph[start.first-1][i] = '#';
  34.                 }
  35.             }
  36.         }
  37.         if(x & 4) {
  38.             if(start.second < m-1) {
  39.                 ret++;
  40.                 for(int i = 0; i < n; i++) {
  41.                     graph[i][start.second+1] = '#';
  42.                 }
  43.             }
  44.         }
  45.         if(x & 8) {
  46.             if(start.second > 0) {
  47.                 ret++;
  48.                 for(int i = 0; i < n; i++) {
  49.                     graph[i][start.second-1] = '#';
  50.                 }
  51.             }
  52.         }
  53.         return ret;
  54.     }
  55.  
  56.     void remove_update(int x, pair<int,int> start) {
  57.         if(x & 1) {
  58.             if(start.first < n-1) {
  59.                 for(int i = 0; i < m; i++) {
  60.                     graph[start.first+1][i] = graph_orig[start.first+1][i];
  61.                 }
  62.             }
  63.         }
  64.         if(x & 2) {
  65.             if(start.first > 0) {
  66.                 for(int i = 0; i < m; i++) {
  67.                     graph[start.first-1][i] = graph_orig[start.first-1][i];
  68.                 }
  69.             }
  70.         }
  71.         if(x & 4) {
  72.             if(start.second < m-1) {
  73.                 for(int i = 0; i < n; i++) {
  74.                     graph[i][start.second+1] = graph_orig[i][start.second+1];
  75.                 }
  76.             }
  77.         }
  78.         if(x & 8) {
  79.             if(start.second > 0) {
  80.                 for(int i = 0; i < n; i++) {
  81.                     graph[i][start.second-1] = graph_orig[i][start.second-1];
  82.                 }
  83.             }
  84.         }
  85.     }
  86.  
  87.     int possible(pair<int,int> p) {
  88.         if(p.first < 0 || p.first >= n || p.second < 0 || p.second >= m) {
  89.             return 2;
  90.         }
  91.         return (graph[p.first][p.second] != '#' && !vis[p]);
  92.     }
  93.  
  94.     bool bfs(pair<int,int> start) {
  95.         while(!q.empty())
  96.             q.pop();
  97.         vis.clear();
  98.         q.push(start);
  99.         vis[start] = 1;
  100.         while(!q.empty()) {
  101.             pair<int,int> curr = q.front();
  102.             q.pop();
  103.             for(int i = 0; i < 4; i++) {
  104.                 pair<int,int> nxt = {curr.first+dx[i],curr.second+dy[i]};
  105.                 int poss = possible(nxt);
  106.                 if(poss == 2) {
  107.                     return true;
  108.                 }
  109.                 if(poss) {
  110.                     vis[nxt] = true;
  111.                     q.push(nxt);
  112.                 }
  113.             }
  114.         }
  115.         return false;
  116.     }
  117.  
  118.     int capture(vector <string> labyrinth) {
  119.         n = labyrinth.size();
  120.         m = labyrinth[0].size();
  121.         for(int i = 0; i < n; i++) {
  122.             for(int j = 0; j < m; j++) {
  123.                 graph[i][j] = graph_orig[i][j] = labyrinth[i][j];
  124.                 if(graph[i][j] == '^') {
  125.                     p_start.push_back({i,j});
  126.                 }
  127.             }
  128.         }
  129.         int mn = -1;
  130.         for(int i = 0; i < 16; i++) {
  131.             for(pair<int,int> start : p_start) {
  132.                 int cost = update(i,start);
  133.                 if(!bfs(start)) {
  134.                     mn = (mn == -1 ? cost:min(mn,cost));
  135.                 }
  136.                 remove_update(i,start);
  137.             }
  138.         }
  139.         return mn;
  140.     }
  141. };
  142.  
  143. int main()
  144. {
  145.     ios_base::sync_with_stdio(false);
  146.     cin.tie(NULL);
  147.     cout.tie(NULL);
  148.     return 0;
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement