고나영 2018. 9. 20. 14:12

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14ABYKADACFAYh&categoryId=AV14ABYKADACFAYh&categoryType=CODE


좌우 먼저 보고 위로 올라갑니당


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
#include <stdio.h>
typedef struct st1
{
    int r; int c;
}ST1;
 
int a[102][102];
ST1 SP, EP;
int WP, RP;
ST1 Queue[102*102];
void InQueue(int rr, int cc) { Queue[WP].r = rr; Queue[WP++].c = cc; }
ST1 DeQueue(void) { return Queue[RP++]; }
int dr[] = { 00-1 };
int dc[] = { -110 };
 
int tc;
 
void input(void)
{
    scanf("%d"&tc);
    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 100; j++)
        {
            scanf("%d"&a[i][j]);
            if (a[i][j] == 2)
            {
                EP.r = i; 
                EP.c = j;
            }
        }
    }
}
 
ST1 BFS(int r, int c)
{
    int i, nr, nc;
    ST1 out;
 
    WP = 0;
    RP = 0;
 
    InQueue(r, c);
    //a[r][c] = 2;
 
    while (RP < WP)
    {
        out = DeQueue();
        for (i = 0; i < 3; i++)
        {
            nr = out.r + dr[i];
            nc = out.c + dc[i];
            if (nr < 0 || nc < 0 || nr >= 100 || nc >= 100continue;
            if (a[nr][nc] == 1)
            {
                InQueue(nr, nc);
                a[nr][nc] = 2;
                break;
            }
        }
    }
    return Queue[WP-1];
}
 
int main(void)
{
    for (tc = 1; tc <= 10; tc++)
    {
        ST1 temp;
 
        input();
        temp = BFS(EP.r, EP.c);
        //output();
        printf("#%d %d\n", tc, temp.c);
    }
    return 0;
}
 
cs