-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.c
97 lines (84 loc) · 2.76 KB
/
Maze.c
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
#include <stdio.h>
#define M 12 // 세로:11
#define N 16 // 가로:15
#define MAX M*N
enum boolean{false, true}; // 0:false, 1:true
int top = -1;
int stack[MAX][3] = {0,};
enum boolean push(int x, int y, int d){
if(top>=MAX-1){
printf("stackoverflow\n");
return false;
}
top = top+1;
stack[top][0] = x;
stack[top][1] = y;
stack[top][2] = d;
return true;
}
enum boolean pop(int *x, int *y, int *d){
if(top==-1){
printf("stack underflow\n");
return false;
}
*x = stack[top][0];
*y = stack[top][1];
*d = stack[top][2];
top = top-1;
return true;
}
enum boolean mazeSearch(int maze[M][N]){
int in_x, in_y, out_x, out_y;
int view_x, view_y, current_x, current_y;
int d = 0; // d는 이동할 수 있는 0~8 방향 중 하나로 0에서 출발
//0방향은 우, 1방향은 우하, 2방향은 하, 3방향은 좌하, 5방향은 좌상, 6방향은 상, 7방향은 우상
int move[8][2] = {{0,1},{1,1},{1,0},{1,-1,},{0,-1},{-1,-1},{-1,0},{-1,1}};
printf("출발 행 : "); scanf_s("%d", &in_x);
printf("출발 열 : "); scanf_s("%d", &in_y);
printf("도착 행 : "); scanf_s("%d", &out_x);
printf("도착 열 : "); scanf_s("%d", &out_y);
current_x = in_x;
current_y = in_y;
while(1){
while(d < 8){
view_x = current_x + move[d][0];
view_y = current_y + move[d][1];
if(view_x>=0&&view_x<M&&view_y>=0&&view_y<N&&maze[view_x][view_y]==1){
maze[current_x][current_y] = 9; // 미로 표시
if(!push(current_x, current_y, d+1)) return false;
current_x = view_x;
current_y = view_y;
if(current_x==out_x&¤t_y==out_y){
maze[current_x][current_y] = 9;
return true;
}
d = 0;
}
else d++;
}
maze[current_x][current_y] = 2; // 돌아온 경로 표시
if(pop(¤t_x, ¤t_y, &d) == false) return false;
}
}
int main(void){
// 0은 벽, 1은 갈 수 있는 통로
int maze[M][N] = {{1,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0},{0,1,1,0,0,0,1,0,0,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,0,1,0,0,1},{1,0,0,0,0,1,0,0,0,0,0,1,1,1,1,0},
{1,1,0,0,0,0,1,0,1,1,1,0,0,0,1,0},{0,0,1,0,0,1,0,1,0,0,1,1,0,0,0,1},
{0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0},{1,0,0,1,0,1,1,1,1,1,0,1,1,0,0,0},
{1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0},{0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,1},
{1,1,0,0,0,1,1,1,1,1,0,0,1,1,0,1},{1,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1}};
int i,j;
if(mazeSearch(maze)==false) printf("출구까지 미로 없음!\n\n");
else{
printf("\n미로 찾기 완료\n");
for(i = 0; i < M; i++){
for(j = 0; j < N; j++)
if(maze[i][j] == 9) printf("* "); // 미로이면 * 출력
else printf("%d ", maze[i][j] == 0 ? 0:1); // 미로 아니면 초기 데이터 출력
printf("\n");
}
}
printf("\n");
return 0;
}