ann8497

wormholes Samsung

Aug 21st, 2019
4,388
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 1 0
  1. /*
  2. wormhole question
  3.  
  4. test case
  5. 1
  6. 0 0
  7. 100 100
  8. 3
  9. 1 2 120 120 2
  10. 4 5 120 100 3
  11. 6 8 150 180 4
  12.  
  13. answer = 32;
  14.  
  15. test case 2
  16. 1
  17. 0 0
  18. 100 100
  19. 3
  20. 1 2 120 120 2
  21. 4 5 120 100 3
  22. 6 8 102 102 4
  23.  
  24. answer 22
  25.  
  26.  tested verified on test cases
  27. */
  28.  
  29. #include<iostream>
  30. using namespace std;
  31.  
  32. int n;
  33. int ans;
  34. bool visited[100];
  35.  
  36. struct hole{
  37.     int x1,y1,x2,y2,cost;
  38. };
  39.  
  40. struct point{
  41.     int x,y;
  42. };
  43.  
  44. int calculate(point src, point dest){
  45.     return (abs(src.x-dest.x) + abs(src.y-dest.y));
  46. }
  47.  
  48.  
  49. void solve(point src, point dest, hole a[], int cost){
  50.    
  51.    
  52.     int tempans = cost + calculate(src,dest);
  53.     cout<<tempans<<endl;
  54.     ans = min(ans,tempans);
  55.    
  56.     for(int i = 0; i<n; i++){
  57.        
  58.         if(!visited[i]){
  59.            
  60.             visited[i] = true;
  61.            
  62.            
  63.             point temp1,temp2;int val;
  64.            
  65.             temp1.x = a[i].x1; temp1.y = a[i].y1;
  66.             temp2.x = a[i].x2; temp2.y = a[i].y2;
  67.            
  68.            
  69.             val =  calculate(src,temp1);
  70.             solve(temp2, dest, a, cost + val + a[i].cost);
  71.             /* jab x1 le rha h to x2 k cordinates aur jab x1 le rha h to x1 k cordinates*/
  72.             val = calculate(src,temp2);
  73.             solve(temp1, dest, a, cost + val + a[i].cost);
  74.            
  75.            
  76.             visited[i] = false;
  77.         }
  78.        
  79.     }
  80.    
  81. }
  82.  
  83. int main(){
  84.    
  85.     int t;
  86.     cin>>t;
  87.     while(t--){
  88.        
  89.         point src,dest;
  90.         cin>>src.x>>src.y>>dest.x>>dest.y;
  91.         cin>>n;
  92.         hole a[n];
  93.         for(int i = 0; i<n; i++)
  94.         cin>>a[i].x1>>a[i].y1>>a[i].x2>>a[i].y2>>a[i].cost;
  95.         ans = 1000000;
  96.         for(int i = 0; i<100; i++)visited[i] = false;
  97.         solve(src,dest,a,0);
  98.         cout<<ans<<endl;
  99.     }
  100.    
  101.   return 0;
  102. }
Add Comment
Please, Sign In to add comment