Advertisement
Mohammad_Dipu_Sultan

Kim and Refrigerators

Oct 15th, 2023
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int a[14], b[14], n, ans;
  5. vector<int>seen;
  6. void solve(int cur, int ind, int cost){
  7.  
  8.     if(ind == n){
  9.         int x = abs(a[cur] - a[1]) + abs(b[cur]-b[1]);
  10.         ans =  min(ans, x+cost);
  11.         return;
  12.     }
  13.  
  14.     for(int i = 2; i < n+2; i++){
  15.         if(seen[i] == 0){
  16.             seen[i] = 1;
  17.             solve(i, ind+1, cost + abs(a[cur]-a[i]) + abs(b[cur] - b[i]));
  18.             seen[i]  = 0;
  19.         }
  20.     }
  21. }
  22. int main(){
  23.     int tc=10, cases=1;
  24.     while(tc--){
  25.         cin >> n;
  26.         seen.assign(n+2, 0);
  27.         for(int i = 0; i < n + 2; i++){
  28.             cin >> a[i] >> b[i];
  29.         }
  30.  
  31.         ans = INT_MAX;
  32.         solve(0, 0, 0);
  33.         cout << "# "<< cases++ <<" " << ans << endl;
  34.     }
  35.  
  36. }
  37.  
  38. /*5
  39. 0 0 100 100 70 40 30 10 10 5 90 70 50 20
  40. 6
  41. 88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
  42. 5
  43. 0 0 100 100 70 40 30 10 10 5 90 70 50 20
  44. 6
  45. 88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
  46. 5
  47. 0 0 100 100 70 40 30 10 10 5 90 70 50 20
  48. 6
  49. 88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
  50. 5
  51. 0 0 100 100 70 40 30 10 10 5 90 70 50 20
  52. 6
  53. 88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
  54. 5
  55. 0 0 100 100 70 40 30 10 10 5 90 70 50 20
  56. 6
  57. 88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
  58.  
  59. # 1 200
  60. # 2 304
  61. # 3 200
  62. # 4 304
  63. # 5 200
  64. # 6 304
  65. # 7 200
  66. # 8 304
  67. # 9 200
  68. # 10 304
  69. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement