Advertisement
Infidelis

Labirint

Apr 11th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. /*
  2. 0 1 1 0 0 0 0 0 0 0 0 0
  3. 1 0 0 1 1 0 0 0 0 0 0 0
  4. 1 0 0 0 0 1 1 0 0 0 0 0
  5. 0 1 0 1 0 0 0 1 0 0 0 0
  6. 0 1 0 0 0 0 0 0 0 0 0 0
  7. 0 0 1 0 0 0 0 0 1 0 0 0
  8. 0 0 1 0 0 0 0 0 0 1 0 0
  9. 0 0 0 1 0 0 0 0 0 0 0 0
  10. 0 0 0 0 0 1 0 0 0 0 0 0
  11. 0 0 0 0 0 0 1 0 0 0 1 1
  12. 0 0 0 0 0 0 0 0 0 1 0 0
  13. 0 0 0 0 0 0 0 0 0 1 0 0
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <conio.h>
  18. #include <string.h>
  19.  
  20. typedef struct graph {
  21.     int **data;
  22.     int size;
  23.     int finish_edge;
  24.     int *prev;
  25.     int counter;
  26.     int from;
  27.     int start;
  28.     int found;
  29. } graph;
  30.  
  31. void init_size(graph *A) {
  32.     printf("Enter the size of matrix of adjacency: ");
  33.     scanf_s("%d", &A->size);
  34. }
  35.  
  36. void allocate_memory(graph *A, int *mark) {
  37.     A->data = (int**)malloc(A->size * sizeof(int*));
  38.     for (int i = 0; i < A->size; i++) {
  39.         A->data[i] = (int*)malloc(A->size * sizeof(int));
  40.     }
  41.     A->prev = (int*)malloc(sizeof(int));
  42. }
  43.  
  44. void roads(graph *A)
  45. {
  46.     char s[10];
  47.     int i, j;
  48.     while (1)
  49.     {
  50.         printf_s("Do you want to close the road? +/-\n");
  51.         scanf_s("%s", s, (unsigned)_countof(s));
  52.         if (strcmp(s,"+") == 0)
  53.         {
  54.             printf_s("Write down two graph heads which connected to the road:\n");
  55.             scanf_s("%d%d", &i, &j);
  56.             A->data[i][j] = 0;
  57.             A->data[j][i] = 0;
  58.         }
  59.         if (strcmp(s, "-") == 0)
  60.         {
  61.             break;
  62.         }
  63.     }
  64. }
  65.  
  66. void initilisation(graph *A, int *mark) {
  67.     for (int i = 0; i < A->size; i++) {
  68.         printf("Enter the elements of %d row of matrix: \n", i);
  69.         for (int j = 0; j < A->size; j++) {
  70.             scanf_s("%d", &A->data[i][j]);
  71.         }
  72.     }
  73.     roads(A);
  74.     printf_s("Enter the start point: ");
  75.     scanf_s("%d", &A->start);
  76.     printf("Enter the finish point: ");
  77.     scanf_s("%d", &A->finish_edge);
  78.     for (int i = 0; i < A->size; i++) {
  79.         mark[i] = 0;
  80.     }
  81.     A->counter = 0;
  82.     A->from = 0;
  83.     A->found = 0;
  84. }
  85.  
  86. void DFS(int Vi, int *mark, graph *A) {
  87.     mark[Vi] = 1;
  88.     for (int i = 0; i < A->size; i++) {
  89.         if (A->data[Vi][i] && !mark[i])
  90.         {
  91.             if (A->from != 0 && A->prev[A->from] == -1)
  92.             {
  93.                 A->prev[A->from] = Vi;
  94.                 A->from = A->from - 1;
  95.                 A->counter = A->counter - 1;
  96.             }
  97.             else {
  98.                 A->counter++;
  99.                 A->prev = (int*)realloc(A->prev, sizeof(int) * A->counter);
  100.                 A->prev[A->from] = Vi;
  101.             }
  102.             A->from++;
  103.             DFS(i, mark, A);
  104.         }
  105.     }
  106.     if (Vi == A->finish_edge) {
  107.         printf("The path was found: ");
  108.         A->counter++;
  109.         A->prev = (int*)realloc(A->prev, sizeof(int) * A->counter);
  110.         A->prev[A->counter - 1] = Vi;
  111.         for (int i = 0; i < A->counter; i++) {
  112.             printf("%d ", A->prev[i]);
  113.         }
  114.         A->found = 1;
  115.     }
  116.     A->prev[A->from] = -1;
  117.     A->from = A->from - 1;
  118.     A->counter = A->counter - 1;
  119. }
  120.  
  121. void limb(graph *A)
  122. {
  123.     if (A->found == 0)
  124.     {
  125.         printf_s("Way is not exist");
  126.     }
  127. }
  128.  
  129. void memfree(graph * A, int * mark)
  130. {
  131.     for (int i = 0; i < A->size; i++) {
  132.         free(A->data[i]);
  133.     }
  134.     free(A->data);
  135.     free(A->prev);
  136.     free(A);
  137.     free(mark);
  138. }
  139.  
  140. void main() {
  141.     graph *A = (graph*)malloc(sizeof(graph));
  142.     init_size(A);
  143.     int *mark = (int*)malloc(A->size * sizeof(int));
  144.     allocate_memory(A, mark);
  145.     initilisation(A, mark);
  146.     DFS(A->start, mark, A);
  147.     limb(A);
  148.     //memfree(A, mark);
  149.     _getch();
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement