Advertisement
arter97

Untitled

Nov 23rd, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int fuel_city[4][4] = { 0 };
  4.  
  5. /*
  6.  * travelcity() should just calculate the travel length
  7.  * and modify the values passed from the main() function via pointers.
  8.  *
  9.  * No printings should be done.
  10.  */
  11.           /* typo : int *dst  // dst is not passed as pointer */
  12. void travelcity(int *state, int dst, int *remain);
  13.  
  14. int main()
  15. {
  16.     int move_dist = 0, remain_fuel = 400;
  17.     int current_pos = 1, dest = 0;
  18.  
  19.     for (int i = 0; i < 3; i++) {
  20.         for (int j = 1; j < 4; j++) {
  21.             if (i < j) {
  22.                 printf
  23.                     ("Please input fuel cost between #%d city to #%d city\n",
  24.                      i + 1, j + 1);
  25.                 scanf("%d", &fuel_city[i][j]);
  26.  
  27.                 // Array is half-filled diagnoally, fill the rest
  28.                 fuel_city[j][i] = fuel_city[i][j];
  29.             }
  30.         }
  31.     }
  32.  
  33.     while (remain_fuel > 0) {
  34.         printf("Please input the city to travel\n");
  35.         scanf("%d", &dest);
  36.  
  37.         if (dest != -1) {
  38.             if (current_pos != dest && dest < 5) {
  39.                 // Common print whether remain_fuel is > 0 or not
  40.                 printf("The car start from city #%d, ",
  41.                        current_pos);
  42.  
  43.                 travelcity(&current_pos, dest, &remain_fuel);
  44.             } else {
  45.                 printf("Please input correct # of city.\n\n");
  46.  
  47.                 // Go loop again
  48.                 continue;
  49.             }
  50.  
  51.             if (remain_fuel > 0) {
  52.                 printf
  53.                     ("The car arrive in city #%d, "
  54.                      "and remain fuel is %d\n", dest,
  55.                      remain_fuel);
  56.             } else {
  57.                 printf
  58.                     ("The car stopped before reaching the #%d city, "
  59.                      "remain fuel is 0, The travel is end.\n",
  60.                      dest);
  61.             }
  62.         } else {
  63.             printf("remain fuel is %d, The travel is end.\n",
  64.                    remain_fuel);
  65.             break;
  66.         }
  67.     }
  68.  
  69.     return 0;
  70. }
  71.  
  72. void travelcity(int *state, int dst, int *remain)
  73. {
  74. // "Extension Goal" : To disable "Extension Goal" feature, comment from here
  75.     int first_waypoint = 0;
  76.     int second_waypoint = 0;
  77.     int third_waypoint = 0;
  78.  
  79.     int total_fuel = fuel_city[*state - 1][dst - 1];
  80.     // "from" -> i -> j -> k -> "to"
  81.     // Calculate the most efficient route
  82.     for (int i = 1; i < 5; i++) {
  83.         if (*state == i)    // No movement needed
  84.             continue;
  85.         for (int j = 1; j < 5; j++) {
  86.             if (i == j) // No movement needed
  87.                 continue;
  88.             for (int k = 1; k < 5; k++) {
  89.                 int tmp = fuel_city[*state - 1][i - 1]  // "from" -> i
  90.                         + fuel_city[i - 1][j - 1]   // i -> j
  91.                         + fuel_city[j - 1][k - 1]   // j -> k
  92.                         + fuel_city[k - 1][dst - 1];    // k -> dst
  93.  
  94.                 if (tmp < total_fuel) {
  95.                     // More efficient than previous iteration
  96.                     first_waypoint = i;
  97.                     second_waypoint = j;
  98.                     third_waypoint = k;
  99.  
  100.                     total_fuel = tmp;
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     if (total_fuel < fuel_city[*state - 1][dst - 1]) {
  107.         // Fuel can be saved by visiting waypoint(s)
  108.  
  109.         if (*state != first_waypoint && first_waypoint != dst) {
  110.             printf("first waypoint city : #%d, ", first_waypoint);
  111.  
  112.             // Travel to the waypoint
  113.             travelcity(state, first_waypoint, remain);
  114.         }
  115.  
  116.         if (first_waypoint != second_waypoint &&
  117.             second_waypoint != third_waypoint &&
  118.             second_waypoint != dst) {
  119.             printf("second waypoint city : #%d, ", second_waypoint);
  120.  
  121.             // Travel to the waypoint
  122.             travelcity(state, second_waypoint, remain);
  123.         }
  124.  
  125.         if (second_waypoint != third_waypoint && third_waypoint != dst) {
  126.             printf("third waypoint city : #%d, ", third_waypoint);
  127.  
  128.             // Travel to the waypoint
  129.             travelcity(state, third_waypoint, remain);
  130.         }
  131.         // Travel to the final destination
  132.         travelcity(state, dst, remain);
  133.  
  134.         printf("\n");
  135.     } else {
  136. // "Extension Goal" : to here
  137.         // Direct route is the most efficient
  138.         if (*remain < fuel_city[*state - 1][dst - 1]) {
  139.             // Insufficient fuel
  140.             *remain = 0;
  141.         } else {
  142.             // Travel done, "to" is now the new "from"
  143.             *remain -= fuel_city[*state - 1][dst - 1];
  144.             *state = dst;
  145.         }
  146. // "Extension Goal" : and remove this bracket
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement