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

@ -48,21 +48,23 @@ 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 i, j, neighbours_count = 0;
int x, y, node; int x, y, node;
int xses[4] = { 0, 0, 1, -1}; int xses[4] = { 0, 0, 1, -1};
int yses[4] = {-1, 1, 0, 0}; 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); find_x_y_of_the_node(&x, &y, source_nodes[i], number_of_x_cells, number_of_y_cells);
if(x != -1 && y != -1) { if (x != -1 && y != -1) {
for(j = 0; j < 4; j++) { for (j = 0; j < 4; j++) {
if(x + xses[j] >= 0 && x + xses[j] < number_of_x_cells && if (x + xses[j] >= 0 && x + xses[j] < number_of_x_cells &&
y + yses[j] >= 0 && y + yses[j] < number_of_y_cells) { y + yses[j] >= 0 && y + yses[j] < number_of_y_cells) {
node = find_node_by_coords(x + xses[j], y + yses[j], number_of_x_cells, number_of_y_cells); node = find_node_by_coords(x + xses[j], y + yses[j], number_of_x_cells, number_of_y_cells);
if((node != -1) && nodes[node] == 0) { if ((node != -1) && nodes[node] == 0) {
nodes[node] = mark; nodes[node] = mark;
neighbours[++neighbours_count] = node; neighbours[++neighbours_count] = node;
} }
@ -75,25 +77,28 @@ int mark_neighbours_of_the_nodes(int *nodes, int *source_nodes, int *neighbours,
} }
/* 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 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 i, j, k = 1, finish = 0;
@ -101,14 +106,15 @@ int find_path(int *nodes, int source_node, int target_node, int *path,
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, {
if (!mark_neighbours_of_the_nodes(nodes, waves[k - 1], waves[k], k,
number_of_x_cells, number_of_y_cells)) { number_of_x_cells, number_of_y_cells)) {
/* the destination can never be reached */ /* the destination can never be reached */
return 0; return 0;
} }
for(i = 1; i <= waves[k][0]; i++) { for (i = 1; i <= waves[k][0]; i++) {
if(waves[k][i] == target_node) { if (waves[k][i] == target_node) {
finish = 1; finish = 1;
break; break;
} }
@ -118,10 +124,11 @@ int find_path(int *nodes, int source_node, int target_node, int *path,
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; finish = 0;
for(i = 1; i <= waves[j][0]; i++) { for (i = 1; i <= waves[j][0]; i++) {
if(is_neighbours(waves[j][i], path[k - j - 1], if (is_neighbours(waves[j][i], path[k - j - 1],
number_of_x_cells, number_of_y_cells)) { number_of_x_cells, number_of_y_cells)) {
path[k - j] = waves[j][i]; path[k - j] = waves[j][i];
break; break;

View File

@ -3,8 +3,10 @@
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, void find_x_y_of_the_node (int *x, int *y,
int node, int number_of_x_cells, int number_of_y_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); int find_node_of_x_y (int x, int y, int number_of_x_cells);
#endif #endif