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', '1', '1', '1' }, { '1', '0', '0', '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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #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', '1', '1', '1' }, { '1', '0', '0', '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 path_length = 0; void backtracking_load(StackType *s, int r, int c, StackType *sm) { element next, tmp; if ((maze[r - 1][c] == '1' || maze[r - 1][c] == '.') && (maze[r + 1][c] == '1' || maze[r + 1][c] == '.') && (maze[r][c - 1] == '1' || maze[r][c - 1] == '.') && (maze[r][c + 1] == '1' || maze[r][c + 1] == '.')) { // ¸·´Ù¸¥ °÷ next = peek(s); do { tmp = pop(sm); path_length--; if ((abs(tmp.c - next.c) + abs(tmp.r - next.r)) == 1) {// backtracking À§Ä¡ ãÀ½ push(sm, tmp); break; } } while (1); } } void print_path(StackType *sm) { int i; element tmp; printf("Ãⱸ->"); for (i = 0; i < path_length; i++) { tmp = pop(sm); printf("(%d,%d)", tmp.r, tmp.c); } printf("->ÀÔ±¸\n"); } int main(void) { int r, c; StackType s, sm; init_stack(&s); init_stack(&sm); 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); element tmp; tmp.r = r; tmp.c = c; push(&sm, tmp); path_length++; backtracking_load(&s, r, c,&sm); if (is_empty(&s)) { printf("½ÇÆÐ\n"); return; } else here = pop(&s); } printf("¼º°ø\n"); print_path(&sm); return 0; } | cs |