Advertisement
at3107

build-offices

Nov 4th, 2020
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. //This is a simple brute force soln that passed all test cases.
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. #define pi pair<int, int>
  8.  
  9. int minDist(int w, int h, vector<pi> arr){
  10.  
  11.     int res = 0;
  12.  
  13.     // for(auto it: arr) cout<<it.first<<" "<<it.second<<endl;
  14.  
  15.     for(int i = 0; i < h; i++){
  16.         for(int j = 0; j < w; j++){
  17.             int k1 = 10000;
  18.             for(auto it: arr){
  19.                 int k = abs(i - it.first) + abs(j - it.second);
  20.                 k1 = min(k1, k);
  21.             }
  22.             res = max(k1, res);
  23.         }
  24.     }
  25.  
  26.     return res;
  27. }
  28.  
  29.  
  30. int fun(int w, int h, int n) {
  31.  
  32.     vector<pi> points;
  33.  
  34.     int ans = INT_MAX;
  35.     int tot = w*h;
  36.  
  37.     for(int i = 0; i < tot; i++){
  38.         int xi = i/w, yi = i%w;
  39.         points.push_back({xi, yi});
  40.         if(n == 1) ans = min(ans, minDist(w, h, points));
  41.         if(n > 1){
  42.             for(int j = i+1; j < tot; j++){
  43.                 int xj = j/w, yj = j%w;
  44.                 points.push_back({xj, yj});
  45.                 if(n == 2) ans = min(ans, minDist(w, h, points));
  46.                 if(n > 2){
  47.                     for(int k = j+1; k < tot; k++){
  48.                         int xk = k/w, yk = k%w;
  49.                         points.push_back({xk, yk});
  50.                         if(n == 3) ans = min(ans, minDist(w, h, points));
  51.                         if(n > 3){
  52.                             for(int l = k+1; l < tot; l++){
  53.                                 int xl = l/w, yl = l%w;
  54.                                 points.push_back({xl, yl});
  55.                                 if(n == 4) ans = min(ans, minDist(w, h, points));
  56.                                 if(n > 4){
  57.                                     for(int m = l+1; m < tot; m++){
  58.                                         int xm = m/w, ym = m%w;
  59.                                         points.push_back({xm, ym});
  60.                                         ans = min(ans, minDist(w, h, points));
  61.                                         points.pop_back();
  62.                                     }
  63.                                 }
  64.                                 points.pop_back();
  65.                             }
  66.                         }
  67.                         points.pop_back();
  68.                     }
  69.                 }
  70.                 points.pop_back();
  71.             }
  72.         }
  73.         points.pop_back();
  74.     }
  75.  
  76.  
  77.     return ans;
  78. }
  79. int main()
  80. {
  81.     int w,h,n;
  82.     cin>>w>>h>>n;
  83.     cout<<fun(w,h,n);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement