Advertisement
Mohammad_Dipu_Sultan

Warmholes

Oct 15th, 2023
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int x2, y2, n, ans;
  5. int a[105][5];
  6. vector<int>seen;
  7.  
  8. void solve(int x1, int y1, int ind, int cost){
  9.     if(ind == n){
  10.         int x = abs(x1 - x2) + abs(y1-y2);
  11.         ans = min(ans, x+cost);
  12.         return;
  13.     }
  14.  
  15.     int c1 = cost + abs(x1 - a[ind][0]) + abs(y1 - a[ind][1]) + a[ind][4];
  16.     int c2 = cost + abs(x1 - a[ind][2]) + abs(y1 - a[ind][3]) + a[ind][4];
  17.  
  18.     if(c1 <= c2 and c1 < ans){
  19.         solve(a[ind][2], a[ind][3], ind+1, c1);
  20.     }
  21.     else if(c2 < ans){
  22.         solve(a[ind][0], a[ind][1], ind+1, c2);
  23.     }
  24.     if(cost < ans){
  25.         solve(x1, y1, ind+1, cost);
  26.     }
  27. }
  28.  
  29. int main(){
  30.     int tc;
  31.     cin >> tc;
  32.     while(tc--){
  33.        
  34.         cin >> n;
  35.         int x1, y1;
  36.         cin >> x1 >> y1 >> x2 >> y2;
  37.  
  38.         for(int i = 0; i < n; i++){
  39.             cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3] >> a[i][4];
  40.         }
  41.         ans = INT_MAX;
  42.         solve(x1, y1, 0, 0);
  43.         cout << ans << endl;
  44.     }
  45.  
  46.     return 0;
  47.  
  48. }
  49.  
  50. /*1
  51. 3
  52. 0 0 100 100
  53. 1 2 120 120 16
  54. 2 5 120 100 21
  55. 6 8 150 180 16
  56.  
  57. 48
  58. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement