Debian Codegraph @

src/path.c

raw symbols SCIP
1 /* path.c - functions related to finding path between points
2 * on board
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <assert.h>
12
13 /* returns x coordinate of the node
14 number_of_cells: number of cells in the row */
15 int __find_x_of_the_node(int node, int number_of_x_cells, int number_of_y_cells) {
16 assert(number_of_x_cells > 0);
17 return node % number_of_x_cells;
18 }
19
20 /* returns y coordinate of the node
21 number_of_cells: number of cells in the row
22 */
23 int __find_y_of_the_node(int node, int number_of_x_cells, int number_of_y_cells) {
24 assert(number_of_x_cells > 0);
25 return node / number_of_x_cells;
26 }
27
28 /* returns x and y coordinates of the node */
29 void find_x_y_of_the_node(int *x, int *y, int node, int number_of_x_cells, int number_of_y_cells) {
30 *x = __find_x_of_the_node(node, number_of_x_cells, number_of_y_cells);
31 *y = __find_y_of_the_node(node, number_of_x_cells, number_of_y_cells);
32 }
33
34 /* returns node at x and y coordinates */
35 int find_node_of_x_y(int x, int y, int number_of_x_cells) {
36 return y * number_of_x_cells + x;
37 }
38
39 /* find number of the node by its coordinates
40 number_of_cells: number of cells in the row */
41 int find_node_by_coords(int x, int y, int number_of_x_cells, int number_of_y_cells) {
42 return y * number_of_x_cells + x;
43 }
44
45 /* mark all neighbouring nodes of the nodes
46 source_nodes: source nodes
47 neighbours: neighbouring nodes
48 mark: number to mark neighbours
49 number_of_cells: number of cells in the row
50 returns number of marked nodes */
51 int mark_neighbours_of_the_nodes (int *nodes, int *source_nodes, int *neighbours, int mark,
52 int number_of_x_cells, int number_of_y_cells)
53 {
54 int i, j, neighbours_count = 0;
55 int x, y, node;
56 int xses[4] = { 0, 0, 1, -1};
57 int yses[4] = {-1, 1, 0, 0};
58
59 for (i = 1; i <= source_nodes[0]; i++)
60 {
61 find_x_y_of_the_node(&x, &y, source_nodes[i], number_of_x_cells, number_of_y_cells);
62 if (x != -1 && y != -1) {
63 for (j = 0; j < 4; j++) {
64 if (x + xses[j] >= 0 && x + xses[j] < number_of_x_cells &&
65 y + yses[j] >= 0 && y + yses[j] < number_of_y_cells) {
66 node = find_node_by_coords(x + xses[j], y + yses[j], number_of_x_cells, number_of_y_cells);
67 if ((node != -1) && nodes[node] == 0) {
68 nodes[node] = mark;
69 neighbours[++neighbours_count] = node;
70 }
71 }
72 }
73 }
74 }
75 neighbours[0] = neighbours_count;
76 return neighbours_count;
77 }
78
79 /* check if nodes are neighbours */
80 int is_neighbours(int node1, int node2, int xn, int yn)
81 {
82 int x1, y1, x2, y2;
83
84 find_x_y_of_the_node(&x1, &y1, node1, xn, yn);
85 find_x_y_of_the_node(&x2, &y2, node2, xn, yn);
86
87 if ((x1 == x2) && ((y1 == y2 + 1) || (y2 == y1 + 1))) {
88 return 1;
89 } else if ((y1 == y2) && ((x1 == x2 + 1) || (x2 == x1 + 1))) {
90 return 1;
91 }
92 return 0;
93 }
94
95
96 /* find the path between source_node and the target_node
97 result stored in path
98 returns 0 on failure and 1 on success */
99 int find_path (int *nodes, int source_node, int target_node, int *path,
100 int number_of_x_cells, int number_of_y_cells)
101 {
102 int waves[number_of_x_cells * number_of_y_cells][number_of_x_cells * number_of_y_cells];
103 int i, j, k = 1, finish = 0;
104
105 waves[0][0] = 1;
106 waves[0][1] = source_node;
107
108 nodes[source_node] = -1;
109 while (!finish)
110 {
111 if (!mark_neighbours_of_the_nodes(nodes, waves[k - 1], waves[k], k,
112 number_of_x_cells, number_of_y_cells)) {
113 /* the destination can never be reached */
114 return 0;
115 }
116 for (i = 1; i <= waves[k][0]; i++) {
117 if (waves[k][i] == target_node) {
118 finish = 1;
119 break;
120 }
121 }
122 k++;
123 }
124
125 path[0] = k;
126 path[1] = waves[k - 1][i];
127 for (j = k - 2; j > 0; j--)
128 {
129 finish = 0;
130 for (i = 1; i <= waves[j][0]; i++) {
131 if (is_neighbours(waves[j][i], path[k - j - 1],
132 number_of_x_cells, number_of_y_cells)) {
133 path[k - j] = waves[j][i];
134 break;
135 }
136 }
137 }
138
139 return 1;
140 }