lelouche29

fishery_Samsung

Sep 15th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. /*
  2. Given:
  3. Fishing Spots: 1 to N
  4. 3 Gates with gate position and number of fishermen waiting to get in
  5. Distance between consecutive spots = distance between gate and nearest spot = 1 m
  6.  
  7. Fishermen are waiting at the gates to get in and occupy nearest fishing spot. Only 1 gate can be opened at a time and all fishermen of that gate must occupy spots before next gate is open.  
  8. There could be 2 spots closest to the gate. Assign only 1 spot to the last fisherman in such a way that we get minimum walking distance. For rest of the fishermen, ignore and assign any one.
  9.  
  10. Write a program to return sum of minimum distance need to walk for fishermen.
  11.  
  12. Distance is calculated as gate to nearest spot + nearest spot to closest vacant spot.
  13. If the gate is at position 4, then fishermen occupying spot 4 will walk 1 m,
  14. */
  15. #include <iostream>
  16. #define INT_MAX 99999
  17. using namespace std;
  18.  
  19. struct Gate{
  20.     int loc,men;
  21. }gate[4];
  22.  
  23. int n;
  24. bool visited[100];
  25. int ans=INT_MAX;
  26.  
  27. //findign nearest left position
  28. int findLeft(int indx){
  29.     for(int i=indx; i>0; i--)
  30.         if(visited[i]==false) return i;
  31.     return INT_MAX;
  32. }
  33.  
  34. //finding nearest right position
  35. int findRight(int indx){
  36.     for(int i=indx+1; i<=n; i++)
  37.         if(visited[i]==false) return i;
  38.     return INT_MAX;
  39. }
  40.  
  41.  
  42. void solve(int x, int y, int z, int cur_pos, int cost){
  43.     if(cost> ans) return;
  44.    
  45.     //base condition
  46.     //when all men have went through 1 gate change gate
  47.     if(gate[cur_pos].men == 0){
  48.       if(cur_pos == x)cur_pos = y;
  49.       else if(cur_pos == y)cur_pos = z;
  50.       else{
  51.           ans = min(ans,cost);
  52.           return;
  53.       }
  54.     }
  55.  
  56.     //nearest left vacant spot
  57.     int left = findLeft(gate[cur_pos].loc);
  58.     int ll = abs(gate[cur_pos].loc - left) +1;
  59.  
  60.     //nearest right vacant spot
  61.     int right = findRight(gate[cur_pos].loc);
  62.     int rr = abs(gate[cur_pos].loc -right) +1;
  63.  
  64.     bool goLeft=true , goRight=true;
  65.  
  66.     if(ll>rr) goLeft=false;
  67.     else if(rr>ll) goRight=false;
  68.    
  69.     //goint in the direction where the distance is less
  70.     if(goLeft){
  71.         gate[cur_pos].men-=1;
  72.         visited[left]=true;
  73.         solve(x,y,z,cur_pos,cost + ll);
  74.         visited[left]=false;
  75.         gate[cur_pos].men+=1;
  76.     }
  77.     if(goRight){
  78.         gate[cur_pos].men-=1;
  79.         visited[right]=true;
  80.         solve(x,y,z,cur_pos,cost + rr);
  81.         visited[right]=false;
  82.         gate[cur_pos].men+=1;
  83.     }
  84. }
  85.  
  86. int main() {
  87.     int t;
  88.     cin>>t;
  89.     while(t--){
  90.         cin>>n;
  91.  
  92.         for(int i=1; i<=3; i++)
  93.             cin>>gate[i].loc>>gate[i].men;
  94.        
  95.         for(int i=0; i<100; i++)
  96.             visited[i]=false;
  97.  
  98.         ans=INT_MAX;
  99.         //calling for all permutations of gate opening
  100.         solve(1,2,3,1,0);
  101.         solve(1,3,2,1,0);
  102.         solve(2,1,3,2,0);
  103.         solve(2,3,1,2,0);
  104.         solve(3,1,2,3,0);
  105.         solve(3,2,1,3,0);
  106.  
  107.         cout<<ans<<endl;
  108.     }
  109.     return 0;
  110. }
  111.  
  112.  
  113.  
  114. /* TestCases
  115. 5
  116. 10
  117. 4 5
  118. 6 2
  119. 10 2
  120. 10
  121. 8 5
  122. 9 1
  123. 10 2
  124. 24
  125. 15 3
  126. 20 4
  127. 23 7
  128. 39
  129. 17 8
  130. 30 5
  131. 31 9
  132. 60
  133. 57 12
  134. 31 19
  135. 38 16
  136.  
  137.  
  138.  
  139. outputs
  140. 18
  141. 25
  142. 57
  143. 86
  144. 339
  145. */
Advertisement
Add Comment
Please, Sign In to add comment