15int init_stack(
t_stack *stack,
int initial_capacity)
17 stack->data = (
t_point *)malloc(initial_capacity *
sizeof(
t_point));
18 if (stack->data == NULL)
20 perror(
"Failed to allocate stack\n");
21 return (EXIT_FAILURE);
24 stack->capacity = initial_capacity;
25 return (EXIT_SUCCESS);
33static int push_neighbors_onto_stack(
int x,
int y,
t_stack *stack)
35 static int dx[4] = {0, 0, -1, 1};
36 static int dy[4] = {-1, 1, 0, 0};
46 if (push(stack, (
t_point){new_x, new_y}) != EXIT_SUCCESS)
47 return (EXIT_FAILURE);
50 return (EXIT_SUCCESS);
62int check_grid_is_not_visited(
t_point current,
t_map *map,
63 t_bool *is_surrounded,
bool **visited)
65 if (current.x < 0 || current.x >= map->width
66 || current.y < 0 || current.y >= map->height)
68 *is_surrounded = ENUM_FALSE;
71 if (visited[current.y][current.x] ==
true
72 || map->data[current.y][current.x] ==
'1')
74 if (map->data[current.y][current.x] !=
'0'
75 && map->data[current.y][current.x] !=
'1')
76 *is_surrounded = ENUM_FALSE;
79 return (EXIT_SUCCESS);
82int initialize_for_flood_fill(
t_stack *stack,
int start_x,
int start_y)
84 if (init_stack(stack, DFLT_STACK_SIZE) != EXIT_SUCCESS)
85 return (EXIT_FAILURE);
86 if (push(stack, (
t_point){start_x, start_y}) != EXIT_SUCCESS)
89 return (EXIT_FAILURE);
91 return (EXIT_SUCCESS);
99t_bool flood_fill(
t_map *map,
int start_x,
int start_y,
bool **visited)
102 t_bool is_surrounded;
105 if (initialize_for_flood_fill(&stack, start_x, start_y) != EXIT_SUCCESS)
107 is_surrounded = ENUM_TRUE;
108 while (is_empty(&stack) ==
false)
110 current = pop(&stack);
111 if (check_grid_is_not_visited(current, map, &is_surrounded, visited)
114 visited[current.y][current.x] =
true;
115 if (push_neighbors_onto_stack(current.x, current.y, &stack)
118 is_surrounded = ENUM_ERROR;
123 return (is_surrounded);