Advertisement
lelouche29

Mr.kim Samsung

Aug 26th, 2019
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. /*
  2. Mr. Kim has to deliver refrigerators to N customers. From the office, he is going to visit all the customers and then return to his home. Each location of the office, his home, and the customers is given in the form of integer coordinates (x,y) (0≤x≤100, 0≤y≤100) . The distance between two arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|, where |x| denotes the absolute value of x; for instance, |3|=|-3|=3. The locations of the office, his home, and the customers are all distinct. You should plan an optimal way to visit all the N customers and return to his among all the possibilities.
  3. You are given the locations of the office, Mr. Kim’s home, and the customers; the number of the customers is in the range of 5 to 10. Write a program that, starting at the office, finds a (the) shortest path visiting all the customers and returning to his home. Your program only have to report the distance of a (the) shortest path.
  4.  
  5. Constraints
  6.  
  7. 5≤N≤10. Each location (x,y) is in a bounded grid, 0≤x≤100, 0≤y≤100, and x, y are integers.
  8.  
  9. Input
  10.  
  11. You are given 10 test cases. Each test case consists of two lines; the first line has N, the number of the customers, and the following line enumerates the locations of the office, Mr. Kim’s home, and the customers in sequence. Each location consists of the coordinates (x,y), which is reprensented by ‘x y’.
  12.  
  13. Output
  14.  
  15. Output the 10 answers in 10 lines. Each line outputs the distance of a (the) shortest path. Each line looks like ‘#x answer’ where x is the index of a test case. ‘#x’ and ‘answer’ are separated by a space.
  16.  
  17. I/O Example
  18.  
  19. Input (20 lines in total. In the first test case, the locations of the office and the home are (0, 0) and (100, 100) respectively, and the locations of the customers are (70, 40), (30, 10), (10, 5), (90, 70), (50, 20).)
  20.  
  21. 5 ← Starting test case #1
  22.  
  23. 0 0 100 100 70 40 30 10 10 5 90 70 50 20
  24.  
  25. 6 ← Starting test case #2
  26.  
  27. 88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
  28.  
  29. 10 ← Starting test case #3
  30.  
  31. 39 9 97 61 35 93 62 64 96 39 36 36 9 59 59 96 61 7 64 43 43 58 1 36
  32.  
  33. Output (10 lines in total)
  34.  
  35. #1 200
  36.  
  37. #2 304
  38.  
  39. #3 366
  40. */
  41.  
  42.  
  43. #include <iostream>
  44. using namespace std;
  45.  
  46. struct point{
  47.     int x,y;
  48. }home,office,cust[100];
  49.  
  50. int n;
  51. int ans;
  52. bool visited[100];
  53.  
  54. int findDistance(point a, point b){
  55.     return abs(a.x - b.x) + abs(a.y - b.y);
  56. }
  57.  
  58. void solve(int cust_visited, int distance_travelled, point cur_pos){
  59.     //base case
  60.     if(cust_visited==n){
  61.         distance_travelled+=findDistance(cur_pos,home);
  62.         ans=min(ans,distance_travelled);
  63.         return;
  64.     }
  65.  
  66.     for(int i=0; i<n; i++){
  67.         if(!visited[i]){
  68.             visited[i]=true;
  69.             int temp=findDistance(cur_pos,cust[i]);
  70.             solve(cust_visited+1, distance_travelled + temp, cust[i] );
  71.  
  72.             //backtrack
  73.             visited[i]=false;
  74.         }
  75.     }
  76. }
  77.  
  78. int main() {
  79.     int t;
  80.     cin>>t;
  81.     while(t--){
  82.         cin>>n;
  83.  
  84.         cin>>office.x>>office.y>>home.x>>home.y;
  85.  
  86.         for(int i=0; i<n; i++)
  87.             cin>>cust[i].x>>cust[i].y;
  88.        
  89.         for(int i=0; i<100; i++)
  90.             visited[i]=false;
  91.  
  92.         ans=999999;
  93.         solve(0,0,office);
  94.         cout<<ans<<endl;
  95.  
  96.        
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement