|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +#include <stdlib.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +#define MAX_SIZE 6 |
| 8 | + |
| 9 | +typedef struct { |
| 10 | + short r; |
| 11 | + short c; |
| 12 | +} element; |
| 13 | + |
| 14 | +typedef struct { |
| 15 | + element data[MAX_SIZE]; |
| 16 | + int top; |
| 17 | +} Stack; |
| 18 | + |
| 19 | +char maze[MAX_SIZE][MAX_SIZE] = { |
| 20 | + {'1', '1', '1', '1', '1', '1'}, |
| 21 | + {'e', '0', '1', '0', '1', '1'}, |
| 22 | + {'1', '0', '0', '0', '1', '1'}, |
| 23 | + {'1', '0', '1', '0', '1', '1'}, |
| 24 | + {'1', '0', '1', '0', '0', 'x'}, |
| 25 | + {'1', '1', '1', '1', '1', '1'}, |
| 26 | +}; |
| 27 | + |
| 28 | +void initialize(Stack* stack) { |
| 29 | + stack->top = -1; |
| 30 | +} |
| 31 | + |
| 32 | +bool isFull(Stack* stack) { |
| 33 | + return stack->top == MAX_SIZE - 1; |
| 34 | +} |
| 35 | + |
| 36 | +bool isEmpty(Stack* stack) { |
| 37 | + return stack->top == -1; |
| 38 | +} |
| 39 | + |
| 40 | +void push(Stack* stack, element data) { |
| 41 | + if (isFull(stack)) { |
| 42 | + exit(1); |
| 43 | + } |
| 44 | + stack->data[++(stack->top)] = data; |
| 45 | +} |
| 46 | + |
| 47 | +element pop(Stack* stack) { |
| 48 | + if (isEmpty(stack)) { |
| 49 | + exit(1); |
| 50 | + } |
| 51 | + return stack->data[(stack->top)--]; |
| 52 | +} |
| 53 | + |
| 54 | +void pushPosition(Stack* s, int r, int c) { |
| 55 | + if (r < 0 || c < 0) return; |
| 56 | + if (maze[r][c] != '1' && maze[r][c] != '.') { |
| 57 | + element pos = { r, c }; |
| 58 | + push(s, pos); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void searchMaze(char maze[MAX_SIZE][MAX_SIZE]) { |
| 63 | + Stack s; |
| 64 | + initialize(&s); |
| 65 | + |
| 66 | + element here, entry = { 1, 0 }; |
| 67 | + here = entry; |
| 68 | + |
| 69 | + int r, c; |
| 70 | + while (maze[here.r][here.c] != 'x') { |
| 71 | + r = here.r; |
| 72 | + c = here.c; |
| 73 | + maze[r][c] = '.'; |
| 74 | + |
| 75 | + pushPosition(&s, r - 1, c); |
| 76 | + pushPosition(&s, r + 1, c); |
| 77 | + pushPosition(&s, r, c - 1); |
| 78 | + pushPosition(&s, r, c + 1); |
| 79 | + |
| 80 | + if (isEmpty(&s)) { |
| 81 | + printf("실패\n"); |
| 82 | + return; |
| 83 | + } |
| 84 | + else { |
| 85 | + here = pop(&s); |
| 86 | + } |
| 87 | + } |
| 88 | + printf("성공\n"); |
| 89 | +} |
| 90 | + |
| 91 | +int main(void) { |
| 92 | + searchMaze(maze); |
| 93 | + return 0; |
| 94 | +} |
0 commit comments