path.c/h: consistent indentation

This commit is contained in:
wdlkmpx 2021-01-14 22:13:53 +08:00
parent 3588163047
commit 7461795c1b
2 changed files with 89 additions and 80 deletions

View File

@ -13,33 +13,33 @@
/* returns x coordinate of the node /* returns x coordinate of the node
number_of_cells: number of cells in the row */ number_of_cells: number of cells in the row */
int __find_x_of_the_node(int node, int number_of_x_cells, int number_of_y_cells) { int __find_x_of_the_node(int node, int number_of_x_cells, int number_of_y_cells) {
assert(number_of_x_cells > 0); assert(number_of_x_cells > 0);
return node % number_of_x_cells; return node % number_of_x_cells;
} }
/* returns y coordinate of the node /* returns y coordinate of the node
number_of_cells: number of cells in the row number_of_cells: number of cells in the row
*/ */
int __find_y_of_the_node(int node, int number_of_x_cells, int number_of_y_cells) { int __find_y_of_the_node(int node, int number_of_x_cells, int number_of_y_cells) {
assert(number_of_x_cells > 0); assert(number_of_x_cells > 0);
return node / number_of_x_cells; return node / number_of_x_cells;
} }
/* returns x and y coordinates of the node */ /* returns x and y coordinates of the node */
void find_x_y_of_the_node(int *x, int *y, int node, int number_of_x_cells, int number_of_y_cells) { void find_x_y_of_the_node(int *x, int *y, int node, int number_of_x_cells, int number_of_y_cells) {
*x = __find_x_of_the_node(node, number_of_x_cells, number_of_y_cells); *x = __find_x_of_the_node(node, number_of_x_cells, number_of_y_cells);
*y = __find_y_of_the_node(node, number_of_x_cells, number_of_y_cells); *y = __find_y_of_the_node(node, number_of_x_cells, number_of_y_cells);
} }
/* returns node at x and y coordinates */ /* returns node at x and y coordinates */
int find_node_of_x_y(int x, int y, int number_of_x_cells) { int find_node_of_x_y(int x, int y, int number_of_x_cells) {
return y * number_of_x_cells + x; return y * number_of_x_cells + x;
} }
/* find number of the node by its coordinates /* find number of the node by its coordinates
number_of_cells: number of cells in the row */ number_of_cells: number of cells in the row */
int find_node_by_coords(int x, int y, int number_of_x_cells, int number_of_y_cells) { int find_node_by_coords(int x, int y, int number_of_x_cells, int number_of_y_cells) {
return y * number_of_x_cells + x; return y * number_of_x_cells + x;
} }
/* mark all neighbouring nodes of the nodes /* mark all neighbouring nodes of the nodes
@ -48,86 +48,93 @@ int find_node_by_coords(int x, int y, int number_of_x_cells, int number_of_y_cel
mark: number to mark neighbours mark: number to mark neighbours
number_of_cells: number of cells in the row number_of_cells: number of cells in the row
returns number of marked nodes */ returns number of marked nodes */
int mark_neighbours_of_the_nodes(int *nodes, int *source_nodes, int *neighbours, int mark, int mark_neighbours_of_the_nodes (int *nodes, int *source_nodes, int *neighbours, int mark,
int number_of_x_cells, int number_of_y_cells) { int number_of_x_cells, int number_of_y_cells)
int i, j, neighbours_count = 0; {
int x, y, node; int i, j, neighbours_count = 0;
int xses[4] = { 0, 0, 1, -1}; int x, y, node;
int yses[4] = {-1, 1, 0, 0}; int xses[4] = { 0, 0, 1, -1};
int yses[4] = {-1, 1, 0, 0};
for(i = 1; i <= source_nodes[0]; i++) { for (i = 1; i <= source_nodes[0]; i++)
find_x_y_of_the_node(&x, &y, source_nodes[i], number_of_x_cells, number_of_y_cells); {
if(x != -1 && y != -1) { find_x_y_of_the_node(&x, &y, source_nodes[i], number_of_x_cells, number_of_y_cells);
for(j = 0; j < 4; j++) { if (x != -1 && y != -1) {
if(x + xses[j] >= 0 && x + xses[j] < number_of_x_cells && for (j = 0; j < 4; j++) {
y + yses[j] >= 0 && y + yses[j] < number_of_y_cells) { if (x + xses[j] >= 0 && x + xses[j] < number_of_x_cells &&
node = find_node_by_coords(x + xses[j], y + yses[j], number_of_x_cells, number_of_y_cells); y + yses[j] >= 0 && y + yses[j] < number_of_y_cells) {
if((node != -1) && nodes[node] == 0) { node = find_node_by_coords(x + xses[j], y + yses[j], number_of_x_cells, number_of_y_cells);
nodes[node] = mark; if ((node != -1) && nodes[node] == 0) {
neighbours[++neighbours_count] = node; nodes[node] = mark;
} neighbours[++neighbours_count] = node;
} }
} }
} }
} }
neighbours[0] = neighbours_count; }
return neighbours_count; neighbours[0] = neighbours_count;
return neighbours_count;
} }
/* check if nodes are neighbours */ /* check if nodes are neighbours */
int is_neighbours(int node1, int node2, int xn, int yn) { int is_neighbours(int node1, int node2, int xn, int yn)
int x1, y1, x2, y2; {
int x1, y1, x2, y2;
find_x_y_of_the_node(&x1, &y1, node1, xn, yn); find_x_y_of_the_node(&x1, &y1, node1, xn, yn);
find_x_y_of_the_node(&x2, &y2, node2, xn, yn); find_x_y_of_the_node(&x2, &y2, node2, xn, yn);
if((x1 == x2) && ((y1 == y2 + 1) || (y2 == y1 + 1))) { if ((x1 == x2) && ((y1 == y2 + 1) || (y2 == y1 + 1))) {
return 1; return 1;
} else if((y1 == y2) && ((x1 == x2 + 1) || (x2 == x1 + 1))) { } else if ((y1 == y2) && ((x1 == x2 + 1) || (x2 == x1 + 1))) {
return 1; return 1;
} }
return 0; return 0;
} }
/* find the path between source_node and the target_node /* find the path between source_node and the target_node
result stored in path result stored in path
returns 0 on failure and 1 on success */ returns 0 on failure and 1 on success */
int find_path(int *nodes, int source_node, int target_node, int *path, int find_path (int *nodes, int source_node, int target_node, int *path,
int number_of_x_cells, int number_of_y_cells) { int number_of_x_cells, int number_of_y_cells)
int waves[number_of_x_cells * number_of_y_cells][number_of_x_cells * number_of_y_cells]; {
int i, j, k = 1, finish = 0; int waves[number_of_x_cells * number_of_y_cells][number_of_x_cells * number_of_y_cells];
int i, j, k = 1, finish = 0;
waves[0][0] = 1; waves[0][0] = 1;
waves[0][1] = source_node; waves[0][1] = source_node;
nodes[source_node] = -1; nodes[source_node] = -1;
while(!finish) { while (!finish)
if(!mark_neighbours_of_the_nodes(nodes, waves[k - 1], waves[k], k, {
number_of_x_cells, number_of_y_cells)) { if (!mark_neighbours_of_the_nodes(nodes, waves[k - 1], waves[k], k,
/* the destination can never be reached */ number_of_x_cells, number_of_y_cells)) {
return 0; /* the destination can never be reached */
} return 0;
for(i = 1; i <= waves[k][0]; i++) { }
if(waves[k][i] == target_node) { for (i = 1; i <= waves[k][0]; i++) {
finish = 1; if (waves[k][i] == target_node) {
break; finish = 1;
} break;
} }
k++; }
} k++;
}
path[0] = k; path[0] = k;
path[1] = waves[k - 1][i]; path[1] = waves[k - 1][i];
for(j = k - 2; j > 0; j--) { for (j = k - 2; j > 0; j--)
finish = 0; {
for(i = 1; i <= waves[j][0]; i++) { finish = 0;
if(is_neighbours(waves[j][i], path[k - j - 1], for (i = 1; i <= waves[j][0]; i++) {
number_of_x_cells, number_of_y_cells)) { if (is_neighbours(waves[j][i], path[k - j - 1],
path[k - j] = waves[j][i]; number_of_x_cells, number_of_y_cells)) {
break; path[k - j] = waves[j][i];
} break;
} }
} }
}
return 1; return 1;
} }

View File

@ -1,10 +1,12 @@
#ifndef __GTKBALLS_PATH_H #ifndef __GTKBALLS_PATH_H
#define __GTKBALLS_PATH_H #define __GTKBALLS_PATH_H
int find_path (int *nodes, int source_node, int target_node, int *path, int find_path (int *nodes, int source_node, int target_node, int *path,
int number_of_x_cells, int number_of_y_cells); int number_of_x_cells, int number_of_y_cells);
void find_x_y_of_the_node (int *x, int *y,
int node, int number_of_x_cells, int number_of_y_cells); void find_x_y_of_the_node (int *x, int *y,
int find_node_of_x_y (int x, int y, int number_of_x_cells); int node, int number_of_x_cells, int number_of_y_cells);
int find_node_of_x_y (int x, int y, int number_of_x_cells);
#endif #endif