|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdbool.h> |
| 4 | + |
| 5 | +#define container_of(ptr, type, member) \ |
| 6 | + ((type *)((char *)(ptr) - (size_t)&(((type *)0)->member))) |
| 7 | + |
| 8 | +#define list_entry(ptr, type, member) \ |
| 9 | + container_of(ptr, type, member) |
| 10 | + |
| 11 | +#define list_first_entry(ptr, type, field) list_entry((ptr)->next, type, field) |
| 12 | +#define list_last_entry(ptr, type, field) list_entry((ptr)->prev, type, field) |
| 13 | + |
| 14 | +#define list_for_each(p, head) \ |
| 15 | + for (p = (head)->next; p != (head); p = p->next) |
| 16 | + |
| 17 | +#define list_for_each_safe(p, n, head) \ |
| 18 | + for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next) |
| 19 | + |
| 20 | +struct list_head { |
| 21 | + struct list_head *next, *prev; |
| 22 | +}; |
| 23 | + |
| 24 | +static inline void |
| 25 | +INIT_LIST_HEAD(struct list_head *list) |
| 26 | +{ |
| 27 | + list->next = list->prev = list; |
| 28 | +} |
| 29 | + |
| 30 | +static inline int |
| 31 | +list_empty(const struct list_head *head) |
| 32 | +{ |
| 33 | + return (head->next == head); |
| 34 | +} |
| 35 | + |
| 36 | +static inline void |
| 37 | +__list_add(struct list_head *new, struct list_head *prev, struct list_head *next) |
| 38 | +{ |
| 39 | + next->prev = new; |
| 40 | + new->next = next; |
| 41 | + new->prev = prev; |
| 42 | + prev->next = new; |
| 43 | +} |
| 44 | + |
| 45 | +static inline void |
| 46 | +list_add(struct list_head *_new, struct list_head *head) |
| 47 | +{ |
| 48 | + __list_add(_new, head, head->next); |
| 49 | +} |
| 50 | + |
| 51 | +static inline void |
| 52 | +list_add_tail(struct list_head *_new, struct list_head *head) |
| 53 | +{ |
| 54 | + __list_add(_new, head->prev, head); |
| 55 | +} |
| 56 | + |
| 57 | +static inline void |
| 58 | +__list_del(struct list_head *entry) |
| 59 | +{ |
| 60 | + entry->next->prev = entry->prev; |
| 61 | + entry->prev->next = entry->next; |
| 62 | +} |
| 63 | + |
| 64 | +static inline void |
| 65 | +list_del(struct list_head *entry) |
| 66 | +{ |
| 67 | + __list_del(entry); |
| 68 | + entry->next = entry->prev = NULL; |
| 69 | +} |
| 70 | + |
| 71 | +struct node { |
| 72 | + struct list_head link; |
| 73 | + int x, y; |
| 74 | +}; |
| 75 | + |
| 76 | +static struct node *node_new(struct list_head *free_list) |
| 77 | +{ |
| 78 | + struct node *new; |
| 79 | + if (list_empty(free_list)) { |
| 80 | + new = malloc(sizeof(*new)); |
| 81 | + } else { |
| 82 | + new = list_first_entry(free_list, struct node, link); |
| 83 | + list_del(&new->link); |
| 84 | + } |
| 85 | + return new; |
| 86 | +} |
| 87 | + |
| 88 | +static void bfs(char **board, int row_size, int col_size, |
| 89 | + struct list_head *queue, struct list_head *free_list) |
| 90 | +{ |
| 91 | + while (!list_empty(queue)) { |
| 92 | + struct node *new; |
| 93 | + struct node *node = list_first_entry(queue, struct node, link); |
| 94 | + board[node->x][node->y] = 'P'; |
| 95 | + |
| 96 | + if (node->x > 0 && board[node->x - 1][node->y] == 'O') { |
| 97 | + new = node_new(free_list); |
| 98 | + new->x = node->x - 1; |
| 99 | + new->y = node->y; |
| 100 | + board[new->x][new->y] = 'P'; |
| 101 | + list_add_tail(&new->link, queue); |
| 102 | + } |
| 103 | + |
| 104 | + if (node->y < col_size - 1 && board[node->x][node->y + 1] == 'O') { |
| 105 | + new = node_new(free_list); |
| 106 | + new->x = node->x; |
| 107 | + new->y = node->y + 1; |
| 108 | + board[new->x][new->y] = 'P'; |
| 109 | + list_add_tail(&new->link, queue); |
| 110 | + } |
| 111 | + |
| 112 | + if (node->x < row_size - 1 && board[node->x + 1][node->y] == 'O') { |
| 113 | + new = node_new(free_list); |
| 114 | + new->x = node->x + 1; |
| 115 | + new->y = node->y; |
| 116 | + board[new->x][new->y] = 'P'; |
| 117 | + list_add_tail(&new->link, queue); |
| 118 | + } |
| 119 | + |
| 120 | + if (node->y > 0 && board[node->x][node->y - 1] == 'O') { |
| 121 | + new = node_new(free_list); |
| 122 | + new->x = node->x; |
| 123 | + new->y = node->y - 1; |
| 124 | + board[new->x][new->y] = 'P'; |
| 125 | + list_add_tail(&new->link, queue); |
| 126 | + } |
| 127 | + |
| 128 | + list_del(&node->link); |
| 129 | + list_add(&node->link, free_list); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +void solve(char** board, int boardRowSize, int boardColSize) |
| 134 | +{ |
| 135 | + int i, j; |
| 136 | + struct node *new; |
| 137 | + struct list_head queue, free_list; |
| 138 | + |
| 139 | + INIT_LIST_HEAD(&queue); |
| 140 | + INIT_LIST_HEAD(&free_list); |
| 141 | + |
| 142 | + for (i = 0; i < boardColSize; i++) { |
| 143 | + if (board[0][i] == 'O') { |
| 144 | + new = node_new(&free_list); |
| 145 | + new->x = 0; |
| 146 | + new->y = i; |
| 147 | + list_add_tail(&new->link, &queue); |
| 148 | + bfs(board, boardRowSize, boardColSize, &queue, &free_list); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + for (i = 0; i < boardColSize; i++) { |
| 153 | + if (board[boardRowSize - 1][i] == 'O') { |
| 154 | + new = node_new(&free_list); |
| 155 | + new->x = boardRowSize - 1; |
| 156 | + new->y = i; |
| 157 | + list_add_tail(&new->link, &queue); |
| 158 | + bfs(board, boardRowSize, boardColSize, &queue, &free_list); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + for (i = 0; i < boardRowSize; i++) { |
| 163 | + if (board[i][0] == 'O') { |
| 164 | + new = node_new(&free_list); |
| 165 | + new->x = i; |
| 166 | + new->y = 0; |
| 167 | + list_add_tail(&new->link, &queue); |
| 168 | + bfs(board, boardRowSize, boardColSize, &queue, &free_list); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + for (i = 0; i < boardRowSize; i++) { |
| 173 | + if (board[i][boardColSize - 1] == 'O') { |
| 174 | + new = node_new(&free_list); |
| 175 | + new->x = i; |
| 176 | + new->y = boardColSize - 1; |
| 177 | + list_add_tail(&new->link, &queue); |
| 178 | + bfs(board, boardRowSize, boardColSize, &queue, &free_list); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + for (i = 0; i < boardRowSize; i++) { |
| 183 | + for (j = 0; j < boardColSize; j++) { |
| 184 | + board[i][j] = board[i][j] == 'P' ? 'O' : 'X'; |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +int main(void) |
| 190 | +{ |
| 191 | + int i, j; |
| 192 | + int row_size = 5; |
| 193 | + int col_size = 5; |
| 194 | + char **board = malloc(row_size * sizeof(char *)); |
| 195 | + board[0] = malloc(col_size); |
| 196 | + board[0][0] = 'X'; |
| 197 | + board[0][1] = 'X'; |
| 198 | + board[0][2] = 'X'; |
| 199 | + board[0][3] = 'X'; |
| 200 | + board[0][4] = 'X'; |
| 201 | + board[1] = malloc(col_size); |
| 202 | + board[1][0] = 'O'; |
| 203 | + board[1][1] = 'X'; |
| 204 | + board[1][2] = 'O'; |
| 205 | + board[1][3] = 'O'; |
| 206 | + board[1][4] = 'X'; |
| 207 | + board[2] = malloc(col_size); |
| 208 | + board[2][0] = 'O'; |
| 209 | + board[2][1] = 'O'; |
| 210 | + board[2][2] = 'X'; |
| 211 | + board[2][3] = 'O'; |
| 212 | + board[2][4] = 'X'; |
| 213 | + board[3] = malloc(col_size); |
| 214 | + board[3][0] = 'X'; |
| 215 | + board[3][1] = 'X'; |
| 216 | + board[3][2] = 'O'; |
| 217 | + board[3][3] = 'X'; |
| 218 | + board[3][4] = 'X'; |
| 219 | + board[4] = malloc(col_size); |
| 220 | + board[4][0] = 'X'; |
| 221 | + board[4][1] = 'X'; |
| 222 | + board[4][2] = 'O'; |
| 223 | + board[4][3] = 'O'; |
| 224 | + board[4][4] = 'X'; |
| 225 | + |
| 226 | + for (i = 0; i < row_size; i++) { |
| 227 | + for (j = 0; j < col_size; j++) { |
| 228 | + printf("%c ", board[i][j]); |
| 229 | + } |
| 230 | + printf("\n"); |
| 231 | + } |
| 232 | + printf("\n"); |
| 233 | + |
| 234 | + solve(board, row_size, col_size); |
| 235 | + for (i = 0; i < row_size; i++) { |
| 236 | + for (j = 0; j < col_size; j++) { |
| 237 | + printf("%c ", board[i][j]); |
| 238 | + } |
| 239 | + printf("\n"); |
| 240 | + } |
| 241 | +} |
0 commit comments