1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STACK_SIZE 100 #define MAZE_SIZE 6 typedef struct { // ±³Ã¼! short r; short c; } element; typedef struct { element data[MAX_STACK_SIZE]; int top; } StackType; // ½ºÅà ÃʱâÈ ÇÔ¼ö void init_stack(StackType *s) { s->top = -1; } // °ø¹é »óÅ °ËÃâ ÇÔ¼ö int is_empty(StackType *s) { return (s->top == -1); } // Æ÷È »óÅ °ËÃâ ÇÔ¼ö int is_full(StackType *s) { return (s->top == (MAX_STACK_SIZE - 1)); } // »ðÀÔÇÔ¼ö void push(StackType *s, element item) { if (is_full(s)) { fprintf(stderr, "½ºÅà Æ÷È ¿¡·¯\n"); return; } else s->data[++(s->top)] = item; } // »èÁ¦ÇÔ¼ö element pop(StackType *s) { if (is_empty(s)) { fprintf(stderr, "½ºÅà °ø¹é ¿¡·¯\n"); exit(1); } else return s->data[(s->top)--]; } // ÇÇÅ©ÇÔ¼ö element peek(StackType *s) { if (is_empty(s)) { fprintf(stderr, "½ºÅà °ø¹é ¿¡·¯\n"); exit(1); } else return s->data[s->top]; } // ===== ½ºÅà ÄÚµåÀÇ ³¡ ===== element here = { 1,0 }, entry = { 1,0 }; char maze[MAZE_SIZE][MAZE_SIZE] = { { '1', '1', '1', '1', '1', '1' }, { 'e', '0', '1', '0', '0', '1' }, { '1', '0', '0', '0', '1', '1' }, { '1', '0', '1', '0', '1', '1' }, { '1', '0', '1', '0', '0', 'x' }, { '1', '1', '1', '1', '1', '1' }, }; // À§Ä¡¸¦ ½ºÅÿ¡ »ðÀÔ void push_loc(StackType *s, int r, int c) { if (r < 0 || c < 0) return; if (maze[r][c] != '1' && maze[r][c] != '.') { element tmp; tmp.r = r; tmp.c = c; push(s, tmp); } } // ¹Ì·Î¸¦ ȸ鿡 Ãâ·ÂÇÑ´Ù. void maze_print(char maze[MAZE_SIZE][MAZE_SIZE]) { printf("\n"); for (int r = 0; r < MAZE_SIZE; r++) { for (int c = 0; c < MAZE_SIZE; c++) { printf("%c", maze[r][c]); } printf("\n"); } } int main(void) { int r, c; StackType s; init_stack(&s); here = entry; while (maze[here.r][here.c] != 'x') { r = here.r; c = here.c; maze[r][c] = '.'; maze_print(maze); push_loc(&s, r - 1, c); push_loc(&s, r + 1, c); push_loc(&s, r, c - 1); push_loc(&s, r, c + 1); if (is_empty(&s)) { printf("½ÇÆÐ\n"); return; } else here = pop(&s); } printf("¼º°ø\n"); return 0; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STACK_SIZE 100 #define MAZE_SIZE 6 typedef struct { // ±³Ã¼! short r; short c; } element; typedef struct { element data[MAX_STACK_SIZE]; int top; } StackType; // ½ºÅà ÃʱâÈ ÇÔ¼ö void init_stack(StackType *s) { s->top = -1; } // °ø¹é »óÅ °ËÃâ ÇÔ¼ö int is_empty(StackType *s) { return (s->top == -1); } // Æ÷È »óÅ °ËÃâ ÇÔ¼ö int is_full(StackType *s) { return (s->top == (MAX_STACK_SIZE - 1)); } // »ðÀÔÇÔ¼ö void push(StackType *s, element item) { if (is_full(s)) { fprintf(stderr, "½ºÅà Æ÷È ¿¡·¯\n"); return; } else s->data[++(s->top)] = item; } // »èÁ¦ÇÔ¼ö element pop(StackType *s) { if (is_empty(s)) { fprintf(stderr, "½ºÅà °ø¹é ¿¡·¯\n"); exit(1); } else return s->data[(s->top)--]; } // ÇÇÅ©ÇÔ¼ö element peek(StackType *s) { if (is_empty(s)) { fprintf(stderr, "½ºÅà °ø¹é ¿¡·¯\n"); exit(1); } else return s->data[s->top]; } // ===== ½ºÅà ÄÚµåÀÇ ³¡ ===== element here = { 1,0 }, entry = { 1,0 }; char maze[MAZE_SIZE][MAZE_SIZE] = { { '1', '1', '1', '1', '1', '1' }, { 'e', '0', '1', '0', '0', '1' }, { '1', '0', '0', '0', '1', '1' }, { '1', '0', '1', '0', '1', '1' }, { '1', '0', '1', '0', '0', 'x' }, { '1', '1', '1', '1', '1', '1' }, }; // À§Ä¡¸¦ ½ºÅÿ¡ »ðÀÔ void push_loc(StackType *s, int r, int c) { if (r < 0 || c < 0) return; if (maze[r][c] != '1' && maze[r][c] != '.') { element tmp; tmp.r = r; tmp.c = c; push(s, tmp); } } // ¹Ì·Î¸¦ ȸ鿡 Ãâ·ÂÇÑ´Ù. void maze_print(char maze[MAZE_SIZE][MAZE_SIZE]) { printf("\n"); for (int r = 0; r < MAZE_SIZE; r++) { for (int c = 0; c < MAZE_SIZE; c++) { printf("%c", maze[r][c]); } printf("\n"); } } void stack_print(StackType *s) { int i; for (i = 5; i > s->top; i--) printf("| |\n"); for (i = s->top; i >= 0; i--) printf("|(%01d,%01d)|\n", s->data[i].r, s->data[i].c); printf("-------\n"); } int main(void) { int r, c; StackType s; init_stack(&s); here = entry; while (maze[here.r][here.c] != 'x') { r = here.r; c = here.c; maze[r][c] = '.'; maze_print(maze); push_loc(&s, r - 1, c); push_loc(&s, r + 1, c); push_loc(&s, r, c - 1); push_loc(&s, r, c + 1); stack_print(&s); if (is_empty(&s)) { printf("½ÇÆÐ\n"); return; } else here = pop(&s); } printf("¼º°ø\n"); return 0; } | cs |