Advertisement
frain8

Untitled

Nov 21st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.56 KB | None | 0 0
  1. /* Dasprog C - 2019
  2. William Handi Wijaya
  3. 0087
  4.  
  5. Program untuk mencari jalan yang dapat dilalui oleh tikus (2)
  6. dalam suatu labirin agar tikus mendapatkan keju (3).
  7. (1) = jalan, (0) = tembok
  8. (Input berasal dari file)
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <stdbool.h>
  15.  
  16. // Prototipe fungsi
  17. int get_maze(int *soal);
  18. void get_start(int x, int y, int tmp);
  19. int valid_input(int tmp);
  20. bool check_move(int *soal, int x, int y, int *hasil, bool *visited);
  21. bool valid_pos(int *soal, int x, int y, bool *visited);
  22. void print_arr(int *arr);
  23.  
  24. // Declarasikan variabel global
  25. int count_2 = 0;
  26. int count_3 = 0;
  27. int size;
  28. int besar;
  29. FILE *inptr = NULL;
  30. int start_x, start_y;
  31. int end_x, end_y;
  32.  
  33. int main(void)
  34. {
  35.     // Buka file
  36.     inptr = fopen("input.txt" , "r");
  37.  
  38.     // Dapatkan ukuran
  39.     fscanf(inptr, "%d", &size);
  40.     besar = size * size;
  41.  
  42.     // Deklarasikan array
  43.     int *soal = malloc(sizeof(int) * besar);
  44.     int *hasil = malloc(sizeof(int) * besar);
  45.  
  46.     // Deklarasikan flag
  47.     bool *visited = malloc(sizeof(bool) * besar);
  48.  
  49.     // Isi tiap array
  50.     memset(hasil, 0, sizeof(int) * besar);
  51.     memset(visited, false, sizeof(bool) * besar);
  52.  
  53.     // Dapatkan maze from user input
  54.     // Jika input invalid, stop program
  55.     if (get_maze(soal) != 0) return 1;
  56.  
  57.     // Jika ada solusi
  58.     if (check_move(soal, start_x, start_y, hasil, visited))
  59.     {
  60.         printf("\nSolusi:\n");
  61.         print_arr(hasil);
  62.     }
  63.     // Jika tidak ada solusi
  64.     else
  65.     {
  66.         printf("\nTidak ada solusi\n");
  67.         return 2;
  68.     }
  69.     fclose(inptr);
  70.     free(soal);
  71.     free(hasil);
  72.     return 0;
  73. }
  74.  
  75. int get_maze(int *soal)
  76. {
  77.     // Untuk setiap column
  78.     for (int y = 0; y < size; y++)
  79.     {
  80.         // Untuk setiap row
  81.         for (int x = 0; x < size; x++)
  82.         {
  83.             // Isi map
  84.             fscanf(inptr, "%d", &soal[x + y * size]);
  85.             int tmp = soal[x + y * size];
  86.  
  87.             // Validasi input
  88.             get_start(x, y, tmp);
  89.             if (valid_input(tmp) != 0) return 1;
  90.         }
  91.     }
  92.     // Jika tidak ada keju/tikus, stop program
  93.     if (count_2 == 0 || count_3 == 0)
  94.     {
  95.         printf("Posisi tikus/keju tidak diketahui\n");
  96.         return 4;
  97.     }
  98.     return 0;
  99. }
  100.  
  101. void get_start(int x, int y, int tmp)
  102. {
  103.     if (tmp == 2 || tmp == 3)
  104.     {
  105.         // Dapatkan start koordinat
  106.         if (tmp == 2)
  107.         {
  108.             count_2++;
  109.             start_x = x;
  110.             start_y = y;
  111.         }
  112.         // Dapatkan stop koordinat
  113.         else if (tmp == 3)
  114.         {
  115.             count_3++;
  116.             end_x = x;
  117.             end_y = y;
  118.         }
  119.     }
  120.     return;
  121. }
  122.  
  123. int valid_input(int tmp)
  124. {
  125.     // Jika tikus/keju lebih dari satu, stop program
  126.     if (count_2 > 1 || count_3 > 1)
  127.     {
  128.         printf("Terlalu banyak tikus/keju\n");
  129.         return 1;
  130.     }
  131.     // Jika user input berada di luar range x (0 <= x <= 3), stop program
  132.     if (tmp < 0 || tmp > 3)
  133.     {
  134.         printf("Invalid Input\n");
  135.         return 2;
  136.     }
  137.     else return 0;
  138. }
  139.  
  140. // Dapatkan solution
  141. bool check_move(int *soal, int x, int y, int *hasil, bool *visited)
  142. {
  143.     // Base case
  144.     // Jika curr_coordinate = stop point, stop loop
  145.     if (x == end_x && y == end_y)
  146.     {
  147.         *(hasil + x + y * size) = *(soal + x + y * size);
  148.         *(visited + x + y * size) = true;
  149.         return true;
  150.     }
  151.     // Recursive case
  152.     if (valid_pos(soal, x, y, visited))
  153.     {
  154.         *(hasil + x + y * size) = *(soal + x + y * size);
  155.         *(visited + x + y * size) = true;
  156.  
  157.         if (check_move(soal, x, y - 1, hasil, visited)) return true; // Check up
  158.         if (check_move(soal, x, y + 1, hasil, visited)) return true; // Check down
  159.         if (check_move(soal, x - 1, y, hasil, visited)) return true; // Check kiri
  160.         if (check_move(soal, x + 1, y, hasil, visited)) return true; // Check kanan
  161.  
  162.         // if every check_move above is false, stop program
  163.         *(hasil + x + y * size) = 0;
  164.         return false;
  165.     }
  166.     else return false;
  167. }
  168.  
  169. // Jika setiap check_move di atas salah, stop program
  170. bool valid_pos(int *soal, int x, int y, bool *visited)
  171. {
  172.     if (0 <= x && x <= size - 1 && 0 <= y && y <= size - 1 && *(soal + x + y * size) != 0 && *(visited + x + y * size) == false)
  173.     {
  174.         return true;
  175.     }
  176.     else return false;
  177. }
  178.  
  179. void print_arr(int *arr)
  180. {
  181.     for (int y = 0; y < size; y++)
  182.     {
  183.         for (int x = 0; x < size; x++)
  184.         {
  185.             printf("%d ", arr[y * size + x]);
  186.         }
  187.         printf("\n");
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement