fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3. int min_dist = 1e9;
  4. int n;
  5. int visited[5];
  6. int wh[5][4];
  7.  
  8. void fun(int s_x, int s_y, int d_x, int d_y, int distance)
  9. {
  10.  
  11. min_dist = min(min_dist, distance+abs(s_x-d_x)+abs(s_y-d_y));
  12. cout << "min dist at this point is " << min_dist << endl;
  13.  
  14.  
  15. for(int i=0; i<n; i++)
  16. {
  17. if(visited[i]==0)
  18. {
  19. cout << "Wormhole " << i << " visited" << endl;
  20. cout << wh[i][0] << " " << wh[i][1] << " " << wh[i][2] << " " << wh[i][3] << " " << wh[i][4] << endl;
  21. visited[i]=1;
  22. int tmp = abs(s_x-wh[i][0])+abs(s_y-wh[i][1])+wh[i][4]+distance;
  23. fun(wh[i][2], wh[i][3], d_x, d_y, tmp);
  24. tmp = abs(s_x-wh[i][2])+abs(s_y-wh[i][3])+wh[i][4]+distance;
  25. fun(wh[i][0], wh[i][1], d_x, d_y, tmp);
  26. visited[i] = 0;
  27. cout << "Wormhole " << i << " UN visited" << endl;
  28. }
  29. }
  30. }
  31.  
  32.  
  33. int main()
  34. {
  35. int s_x, s_y, d_x, d_y;
  36. //int n;
  37. cin >> s_x >> s_y >> d_x >> d_y >> n;
  38. //int wh[n][5];
  39. //int visited[n];
  40. min_dist = 1e9;
  41. int a;
  42. for(int i=0; i<n; i++){
  43. for(int j=0; j<5; j++){
  44. cin >> a;
  45. wh[i][j] = a;
  46. }
  47. }
  48.  
  49. for(int i=0; i<n; i++)
  50. visited[i] = 0;
  51.  
  52. cout << s_x << " " << s_y << " " << d_x << " " << d_y << " " << n <<endl;
  53. fun(s_x, s_y, d_x, d_y, 0);
  54.  
  55. cout << min_dist;
  56.  
  57. return 0;
  58. }
Runtime error #stdin #stdout 0s 4564KB
stdin
0 0 100 100 3
1 2 120 120 5
4 5 120 100 21
6 8 150 180 23
stdout
Standard output is empty