코딩/C언어

미로찾기

런던전통손만두 2019. 9. 17. 15:26
반응형
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
#include <stdio.h>
 
#define WIDTH 10
#define HEIGHT 10
 
int screen[WIDTH][HEIGHT] = { 
    000,-1,-1,-1,-1,-1,-1,-1,
    -1,-10,-1,-1,-1,-1,-1,-1,-1
    -1000000,-1,-1,-1
    -1,-1,-1,-10,-10,-1,-1,-1
    -1,-1,-1,-10,-10,-1,-1,-1
    -1,-1000,-1000,-1
    -1,-1,-1,-10,-10,-1,-1,-1
    -1,-1,-1,-10,-10,-10,-1
    -1,-1,-1,-10,-1,-1,-10,-1
    -1,-1,-1,-1000000 
};
 
void display()
{
    int x, y;
    for (x = 0; x < WIDTH; x++) {
        for (y = 0; y < HEIGHT; y++)
            printf("%3d", screen[x][y]);
        printf("\n");
    }
}
 
int flood_fill(int x, int y, int i)
{
    if (screen[x][y] == 0)
    {
        screen[x][y] = i;
 
        i++;
 
        if (y + 1 < HEIGHT)
            i = flood_fill(x, y + 1, i); // 오른 쪽 3시
        
        if (x + 1 < WIDTH)
            i = flood_fill(x + 1, y, i); // 아래 쪽 6시
        
        if (y - 1 >= 0)
            i = flood_fill(x, y - 1, i); // 왼쪽 9시
        
        if (x - 1 >= 0)
            i = flood_fill(x - 1, y, i); // 위쪽 12시
 
    }
 
    return i;
}
 
int main(void)
{
    int a;
 
    display();
    printf("시작점을 (0, 0)으로 한 미로방문(순서표기):\n");
    a = flood_fill(001);
    printf("\n");
    display();
 
cs

 

결과:

반응형