본문 바로가기
Computer Science

A기실 미로1 / 큐문제에 재귀로

by OKOK 2018. 12. 13.

1. DFS 형식으로 풀이 한 것이네

2. 돌아오면 이전으로 리턴해서 그 값에서 새로이 시작하는 것임

3. 그럼 이전의 maze 들은 어떻게 변화되는 것이지? 어쩃든 그자리는 지나간 자리니 0으로 리셋할 필요가 없구나

4. 다른 풀이들도 비슷하네


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
#include <stdio.h>
 
char maze[16][16];
int end;
 
int func(int x, int y)
{
    if (maze[x][y] == '3')
    {
        end = 1;
        return end;
    }
    if ((!end&& ((maze[x][y + 1== '0'|| (maze[x][y + 1== '3')))
    {
        maze[x][y] = '1';
        func(x, y + 1);
    }
    if ((!end&& ((maze[x + 1][y] == '0'|| (maze[x + 1][y] == '3')))
    {
        maze[x][y] = '1';
        func(x + 1, y);
    }
    if ((!end&& ((maze[x][y - 1== '0'|| (maze[x][y - 1== '3')))
    {
        maze[x][y] = '1';
        func(x, y - 1);
    }
    if ((!end&& ((maze[x - 1][y] == '0'|| (maze[x - 1][y] == '3')))
    {
        maze[x][y] = '1';
        func(x - 1, y);
    }
    return end;
}
 
int main()
{
    int TC = 1, a;
 
    freopen("input.txt""r", stdin);
 
    while (TC != 10)
    {
        end = 0;
        scanf("%d "&TC);
        for (int i = 0; i < 16; i++)
        {
            //          for (int j = 0; j < 16; j++)
            scanf("%s"&maze[i]);
            scanf("%c"&a);
        }
        printf("#%d %d\n", TC, func(11));
    }
    return 0;
}
 
cs


'Computer Science' 카테고리의 다른 글

A기실 암호문3 / 배열? top - >stack?  (0) 2018.12.14
A기실 암호문1/ 리스트를 배열로  (0) 2018.12.14
A기실 암호생성기 / 큐  (0) 2018.12.13
A기실 계산기1 / 스택  (0) 2018.12.13
A 실 기 길찾기 / DFS  (0) 2018.12.13

댓글