Advertisement
lelouche29

wormhole Samsung

Aug 21st, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. /*
  2. There is a source (S) and destination (D) and a spacecraft has to go from S to D. There are N number of wormholes in between which has following properties:
  3.  
  4. Each wormhole has an entry and an exit. Each wormhole is bi-directional i.e. one can enter and exit from any of the ends. The time to cross the wormhole is given and the space craft may or may not use the wormhole to reach D. The time taken to travel outside wormhole between two points (x1, y1) and (x2, y2) is given by a formula |x1 - x2| + |y1 - y2|
  5.  
  6. where, (x1, y1) and (x2, y2) are the co-ordinates of two points. The co-ordinates of S and D are given and we have to find the minimum time to reach D from S.
  7.  
  8. Note: It’s not mandatory to consider all the wormholes.
  9. */
  10.  
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. struct point{
  15.     int x,y;
  16. }src,dest;
  17.  
  18. struct wormhole{
  19.     int s1,s2,d1,d2,cost;
  20. }worm[100];
  21.  
  22. int findDistance(point a, point b){
  23.     return abs(a.x-b.x) + abs(a.y -b.y);
  24. }
  25.  
  26. int n;
  27. int visited[100]={false};
  28. int ans;
  29.  
  30. void solve(point cur_pos, int cost, int worm_left ){
  31.     ans=min(ans,cost + findDistance(cur_pos,dest));
  32.     if(cur_pos.x==dest.x && cur_pos.y==dest.y){
  33.         // cost+=findDistance(cur_pos,dest);
  34.         ans=min(ans,cost);
  35.         return;
  36.     }
  37.  
  38.     for(int i=0; i<n; i++){
  39.         if(visited[i]==false){
  40.             point temp,temp1;
  41.             //stroring the cordinatess of womrhole start
  42.             temp.x=worm[i].s1; temp.y=worm[i].s2;
  43.             //storing the cordinates of ending of wormhole
  44.             temp1.x=worm[i].d1; temp1.y=worm[i].d2;
  45.  
  46.             //distance of going from current position to the start of wormhole
  47.             int cost1=findDistance(cur_pos,temp);
  48.             //distance of going from current position to the end of wormhole
  49.             int cost2=findDistance(cur_pos,temp1);
  50.  
  51.             visited[i]=true;
  52.             //taking wormhole fromt its starting
  53.             solve(temp1, cost + cost1 + worm[i].cost, n);
  54.             //taking wormhole from its end
  55.             solve(temp, cost + cost2 + worm[i].cost, n);
  56.  
  57.             //backtracking
  58.             visited[i]=false;
  59.  
  60.         }
  61.     }
  62. }
  63.  
  64. int main() {
  65.     int t;
  66.     cin>>t;
  67.     while(t--){
  68.         cin>>n;
  69.         cin>>src.x>>src.y>>dest.x>>dest.y;
  70.  
  71.         for(int i=0; i<n; i++){
  72.             cin>>worm[i].s1>>worm[i].s2>>worm[i].d1>>worm[i].d2>>worm[i].cost;
  73.         }
  74.         for(int i=0; i<100; i++)
  75.             visited[i]=false;
  76.  
  77.         ans=findDistance(src,dest);
  78.         solve(src,0,n);
  79.         cout<<ans<<endl;
  80.     }
  81.     return 0;
  82. }
  83.  
  84.  
  85. /*
  86. testcases:-
  87. 5
  88. 0
  89. 0 0 60 60
  90. 1
  91. 0 0 2 0
  92. 1 0 1 2 0
  93. 1
  94. 0 0 60 60
  95. 40 40 20 20 20
  96. 2
  97. 100 50 10 5
  98. 80 40 10 6 10
  99. 80 10 70 40 5
  100. 3
  101. 500 500 1000 1000
  102. 501 501 999 999 1000
  103. 1 1 499 499 100
  104. 1000 999 0 0 200
  105.  
  106. output:-
  107. 120
  108. 2
  109. 100
  110. 41
  111. 305
  112. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement