이건 아무것도 안건드린 코드고

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
#include "stack.h"
#define MAZE_SIZE 20
#define WALL '1'
#define ROAD '0'
#define GOAL 'x'
#define VISITED '.'
#define PATH '#'
int maze_size = 0;
element entry = { 0, 0 };
char maze[MAZE_SIZE][MAZE_SIZE];
// 위치를 스택에 삽입
// 미로 내의 좌표이고 벽이나 이미 방문한 경로가 아니면
// 그것을 element를 만들어서 push
void push_loc(StackType* s, int x, int y)
{
if (x < 0 || y < 0) return;
if (x >= maze_size || y >= maze_size) return;
if (maze[x][y] != WALL && maze[x][y] != VISITED) {
element tmp;
tmp.x = x;
tmp.y = y;
push(s, tmp);
}
}
// 미로를 화면에 출력한다.
void maze_print()
{
printf(" ");
for (int x = 0; x < maze_size; x++) {
printf("%s ", maze[x]); // 한행씩 문자열처럼 출력
}
}
void init_maze(element* entry)
{
printf("maze size = ");
scanf_s("%d", &maze_size);
for (int x = 0; x < maze_size; x++) {
printf("%d행: ", x); // 한 행씩 문자열처럼 입력
scanf_s("%s", maze[x], MAZE_SIZE);
}
}
int main(void)
{
int x, y;
char buf[10];
element here;
StackType s;
init_stack(&s);
init_maze(&entry);
here = entry; // 구조체 지정 (x, y) 값 복사
while (maze[here.x][here.y] != GOAL) {
x = here.x;
y = here.y;
maze[x][y] = VISITED;
gets_s(buf, 10); // 엔터 쳐야 넘어가게 잠깐 멈춤 기능
maze_print(maze); // 현재 미로 상태 출력
push_loc(&s, x - 1, y); // 상
push_loc(&s, x + 1, y); // 하
push_loc(&s, x, y - 1); // 좌
push_loc(&s, x, y + 1); // 우.. 갈수 있는 곳을 모두 push
if (is_empty(&s)) { // 더이상 갈곳이 없으면 길이 없다는 뜻
printf("실패 ");
return 1;
}
here = pop(&s); // 다음에 갈 곳을 꺼냄 (우좌하상 순서로 방문)
}
printf("성공 ");
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
#include "stack.h"
#define MAZE_SIZE 20
#define WALL '1'
#define ROAD '0'
#define GOAL 'x'
#define VISITED '#'
#define PATH '.'
#define END 'G'
int maze_size = 0;
element entry = { 0, 0 };
char maze[MAZE_SIZE][MAZE_SIZE];
// 위치를 스택에 삽입
// 미로 내의 좌표이고 벽이나 이미 방문한 경로가 아니면
// 그것을 element를 만들어서 push
void push_loc(StackType* s, int x, int y)
{
if (x < 0 || y < 0) return;
if (x >= maze_size || y >= maze_size) return;
if (maze[x][y] != WALL && maze[x][y] != VISITED ) {
element tmp;
tmp.x = x;
tmp.y = y;
push(s, tmp);
}
}
// 미로를 화면에 출력한다.
void maze_print()
{
printf(" ");
for (int x = 0; x < maze_size; x++) {
for(int y = 0;y <maze_size; y++)
{
if (maze[x][y] == GOAL) {
maze[x][y] = END;
}
if(y==0)
printf("%s ", maze[x]); // 한행씩 문자열처럼 출력
}
}
}
void init_maze(element* entry)
{
printf("maze size = ");
scanf_s("%d", &maze_size);
for (int x = 0; x < maze_size; x++) {
printf("%d행: ", x); // 한 행씩 문자열처럼 입력
scanf_s("%s", maze[x], MAZE_SIZE);
}
}
int neighbor(element next, element here) {
StackType s;
StackType s2;
while (maze[here.x][here.y] != maze[next.x - 1][next.y] || maze[here.x][here.y] != maze[next.x + 1][next.y] || maze[here.x][here.y] != maze[next.x][next.y - 1] || maze[here.x][here.y] != maze[next.x][next.y + 1]) {
maze[here.x][here.y] = PATH;
here = pop(&s2);
}
}
int main(void)
{
int x, y;
char buf[10];
element here;
element next;
StackType s;
StackType s2;
init_stack(&s);
init_stack(&s2);
init_maze(&entry);
here = entry; // 구조체 지정 (x, y) 값 복사
while (maze[here.x][here.y] != END) {
push_loc(&s2, here.x, here.y);
x = here.x;
y = here.y;
maze[x][y] = VISITED;
gets_s(buf, 10); // 엔터 쳐야 넘어가게 잠깐 멈춤 기능
maze_print(maze); // 현재 미로 상태 출력
push_loc(&s, x - 1, y); // 상
push_loc(&s, x + 1, y); // 하
push_loc(&s, x, y - 1); // 좌
push_loc(&s, x, y + 1); // 우.. 갈수 있는 곳을 모두 push
if (is_empty(&s)) { // 더이상 갈곳이 없으면 길이 없다는 뜻
printf("실패 ");
return 1;
}
next = pop(&s);
int neighbor(element next, element here);
here = next;
}
printf("성공 ");
return 0;
}
cs

이번엔 걍 코드 전체입니다

stack.h는 기본적인 push pop 이런거 선언해놓은 파일입니다,,,,,

c언어 스택으로 미로찾기하는 과제입니다,,,,


ex)

입력:

6


001011

101001

001100

100000

001110

10011x



출력:


6


##1011

1#1001

.#1100

1#####

00111#

10011G


이렇게 [0][0]에서 시작해서 x에 도달할때까지 지나온길을 #, 들렸다가 막다른길이라서 되돌아온길을 '.'으로 표시하는 코드입니다.






시작위치를 here에 넣는다.

스택을 초기화한다.

here가 목표지점이 아닐 동안 반복

맵에서 here방문으로 표시

here에서 상하좌우로 갈 수 있는 곳을 스택에 푸시한다.

스택이 비어있으면 실패

here = pop

목표지점에 도달하면 성공.

스택을 출력하고 종료한다.



이게 맨처음 주어진 알고리즘이고



시작위치를 here에 넣는다.

갈길스택을 초기화한다. 경로스택을 초기화한다.

here가 목표지점이 아닐 동안 반복

맵에서 here경로로 표시

경로스택에 here를 푸시하고

here에서 상하좌우로 갈 수 있는 곳을 갈길스택에 푸시한다.

갈길스택이 비어있으면 실패

next <- 갈길스택에서 팝한다.

herenext의 인접한 칸이 될 때까지

here방문으로 표시

here <- 경로스택을 계속 팝한다.

here를 경로스택에 다시 푸시한다.

here = next

목표지점에 도달하면 성공.

스택을 출력하고 종료한다.


이렇게 수정하라고 과제를 내주셨는데 밑줄친 부분을 int neighbor로 구현하라고 하셧읍니다,,,

주위에 물어보니 int neighbor를 아예 첫단추부터 잘못 잠궛다면서 처음부터 다시 짜는게 낫다고 햇는데 어디가 잘못된지도 모르겠고 어떻게 수정해야할지 감도 안와서 질문좀 드리겟읍니다,,,,