Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 0 1 1 0 0 0 0 0 0 0 0 0
- 1 0 0 1 1 0 0 0 0 0 0 0
- 1 0 0 0 0 1 1 0 0 0 0 0
- 0 1 0 1 0 0 0 1 0 0 0 0
- 0 1 0 0 0 0 0 0 0 0 0 0
- 0 0 1 0 0 0 0 0 1 0 0 0
- 0 0 1 0 0 0 0 0 0 1 0 0
- 0 0 0 1 0 0 0 0 0 0 0 0
- 0 0 0 0 0 1 0 0 0 0 0 0
- 0 0 0 0 0 0 1 0 0 0 1 1
- 0 0 0 0 0 0 0 0 0 1 0 0
- 0 0 0 0 0 0 0 0 0 1 0 0
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <string.h>
- typedef struct graph {
- int **data;
- int size;
- int finish_edge;
- int *prev;
- int counter;
- int from;
- int start;
- int found;
- } graph;
- void init_size(graph *A) {
- printf("Enter the size of matrix of adjacency: ");
- scanf_s("%d", &A->size);
- }
- void allocate_memory(graph *A, int *mark) {
- A->data = (int**)malloc(A->size * sizeof(int*));
- for (int i = 0; i < A->size; i++) {
- A->data[i] = (int*)malloc(A->size * sizeof(int));
- }
- A->prev = (int*)malloc(sizeof(int));
- }
- void roads(graph *A)
- {
- char s[10];
- int i, j;
- while (1)
- {
- printf_s("Do you want to close the road? +/-\n");
- scanf_s("%s", s, (unsigned)_countof(s));
- if (strcmp(s,"+") == 0)
- {
- printf_s("Write down two graph heads which connected to the road:\n");
- scanf_s("%d%d", &i, &j);
- A->data[i][j] = 0;
- A->data[j][i] = 0;
- }
- if (strcmp(s, "-") == 0)
- {
- break;
- }
- }
- }
- void initilisation(graph *A, int *mark) {
- for (int i = 0; i < A->size; i++) {
- printf("Enter the elements of %d row of matrix: \n", i);
- for (int j = 0; j < A->size; j++) {
- scanf_s("%d", &A->data[i][j]);
- }
- }
- roads(A);
- printf_s("Enter the start point: ");
- scanf_s("%d", &A->start);
- printf("Enter the finish point: ");
- scanf_s("%d", &A->finish_edge);
- for (int i = 0; i < A->size; i++) {
- mark[i] = 0;
- }
- A->counter = 0;
- A->from = 0;
- A->found = 0;
- }
- void DFS(int Vi, int *mark, graph *A) {
- mark[Vi] = 1;
- for (int i = 0; i < A->size; i++) {
- if (A->data[Vi][i] && !mark[i])
- {
- if (A->from != 0 && A->prev[A->from] == -1)
- {
- A->prev[A->from] = Vi;
- A->from = A->from - 1;
- A->counter = A->counter - 1;
- }
- else {
- A->counter++;
- A->prev = (int*)realloc(A->prev, sizeof(int) * A->counter);
- A->prev[A->from] = Vi;
- }
- A->from++;
- DFS(i, mark, A);
- }
- }
- if (Vi == A->finish_edge) {
- printf("The path was found: ");
- A->counter++;
- A->prev = (int*)realloc(A->prev, sizeof(int) * A->counter);
- A->prev[A->counter - 1] = Vi;
- for (int i = 0; i < A->counter; i++) {
- printf("%d ", A->prev[i]);
- }
- A->found = 1;
- }
- A->prev[A->from] = -1;
- A->from = A->from - 1;
- A->counter = A->counter - 1;
- }
- void limb(graph *A)
- {
- if (A->found == 0)
- {
- printf_s("Way is not exist");
- }
- }
- void memfree(graph * A, int * mark)
- {
- for (int i = 0; i < A->size; i++) {
- free(A->data[i]);
- }
- free(A->data);
- free(A->prev);
- free(A);
- free(mark);
- }
- void main() {
- graph *A = (graph*)malloc(sizeof(graph));
- init_size(A);
- int *mark = (int*)malloc(A->size * sizeof(int));
- allocate_memory(A, mark);
- initilisation(A, mark);
- DFS(A->start, mark, A);
- limb(A);
- //memfree(A, mark);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement