Advertisement
_rashed

UVA 10888

Feb 4th, 2023
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /*
  6. Ordered set usage:
  7. order_of_key (k) : Number of items strictly smaller than k.
  8. find_by_order(k) : K-th element in a set (counting from zero).
  9. */
  10.  
  11. #include <ext/pb_ds/assoc_container.hpp>
  12. #include <ext/pb_ds/tree_policy.hpp>
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  15.  
  16. const int OO = 1e9;
  17. const double EPS = 1e-9;
  18.  
  19. int dx[4] = {1,-1,0,0};
  20. int dy[4] = {0,0,1,-1};
  21.  
  22. int h,w;
  23. char grid[40][40];
  24. int dist[15][15];
  25. int x_sz = 0;
  26.  
  27. bool valid(int r, int c) {
  28.     return (r >= 0 && r < h && c >= 0 && c < w && grid[r][c] != '#');
  29. }
  30.  
  31. int mem[15][32799];
  32. int solve(int idx, int msk) {
  33.     if(idx == x_sz) {
  34.         return 0;
  35.     }
  36.     if(mem[idx][msk] != -1) {
  37.         return mem[idx][msk];
  38.     }
  39.     int &ret = mem[idx][msk];
  40.     ret = 1e9;
  41.     for(int i = 0; i < x_sz; i++) {
  42.         int cm = (1 << i);
  43.         //cout << "dist[" << idx << "][" << i << "] = " << dist[idx][i] << "\n";
  44.         if(!(msk&cm) && dist[idx][i] != 1e9) {
  45.             int new_msk = msk | cm;
  46.             ret = min(ret,dist[idx][i]+solve(idx+1,new_msk));
  47.         }
  48.     }
  49.     //cout << "at idx = " << idx << " msk = " << msk << " ret = " << ret << "\n";
  50.     return ret;
  51. }
  52.  
  53. int main()
  54. {
  55.     ios_base::sync_with_stdio(NULL);
  56.     cin.tie(0);
  57.     int t;
  58.     cin >> t;
  59.     while(t--) {
  60.         for(int i = 0; i < 15; i++) {
  61.             for(int j = 0; j < 15; j++) {
  62.                 dist[i][j] = 1e9;
  63.             }
  64.         }
  65.  
  66.         map<pair<int,int>,int> mp;
  67.         vector<pair<int,int>> bv;
  68.  
  69.         cin >> h >> w;
  70.         x_sz = 0;
  71.         for(int i = 0; i < h; i++) {
  72.             for(int j = 0; j < w; j++) {
  73.                 cin >> grid[i][j];
  74.                 if(grid[i][j] == 'X') {
  75.                     mp[{i,j}] = x_sz++;
  76.                 }
  77.                 else if(grid[i][j] == 'B') {
  78.                     bv.push_back({i,j});
  79.                 }
  80.             }
  81.         }
  82.         for(int i = 0; i < bv.size(); i++) {
  83.             bool vis[40][40] = {0};
  84.             queue<pair<pair<int,int>,int>> q;
  85.             vis[bv[i].first][bv[i].second] = 1;
  86.             q.push({bv[i],0});
  87.             while(!q.empty()) {
  88.                 pair<pair<int,int>,int> curr = q.front();
  89.                 q.pop();
  90.                 int r = curr.first.first;
  91.                 int c = curr.first.second;
  92.                 int w = curr.second;
  93.  
  94.                 for(int j = 0; j < 4; j++) {
  95.                     int x = r+dx[j];
  96.                     int y = c+dy[j];
  97.                     if(valid(x,y) && !vis[x][y]) {
  98.                         //cout << "here2\n";
  99.                         q.push({{x,y},w+1});
  100.                         vis[x][y] = 1;
  101.                         if(grid[x][y] == 'X') {
  102.                             //cout << "here at mp[{x,y}] = " << mp[{x,y}] << " i = " << i << "\n";
  103.                             dist[mp[{x,y}]][i] = w+1;
  104.                         }
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         for(int i = 0; i < x_sz; i++) {
  110.             for(int j = 0; j < 32799; j++) {
  111.                 mem[i][j] = -1;
  112.             }
  113.         }
  114.         cout << solve(0,0) << "\n";
  115.     }
  116.     return 0;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement