본문 바로가기
Computer Science

APS 기본 실습 Ladder2 / 배열

by OKOK 2018. 12. 13.

1. 옆으로 가는 순서 봄

2. 가장 짧은 거리로 이동하는 것

3. 찾으면 됨

4. 시작하는 순서로 해서, 하나씩 내려오고, 오른쪽으로 이동하는 것을 나타냄

5. 조건을 걸어두고, 그것을 저장하는 변수를 사용하면 됨 


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
#include <stdio.h>
 
#define DEFAULT 0
#define NORTH 1
#define SOUTH 2
#define EAST 3
#define WEST 4
 
#define MAX 10
 
int ladder[MAX + 1][MAX + 1];
int curX, curY;
int startX, startY;
int endY;
int main(void)
{
    int test_case;
    int T;
    freopen("input.txt""r", stdin);
    setbuf(stdout, NULL);
 
    for (test_case = 1; test_case <= 10++test_case)
    {
        scanf("%d"&T);
        for (int i = 0; i < MAX; i++)
        {
            for (int j = 0; j < MAX; j++)
            {
                scanf("%1d"&ladder[i][j]);
            }
        }
 
        curX = 0;
        curY = 0;
 
        int from = DEFAULT;
        int move = 0;
        int move_min = 10000;
        int min_Y = 0;
        for (int j = 0; j < MAX; j++)
        {
 
            if (ladder[0][j] == 1)
            {
                int while_break = 1;
                startY = j;
                curX = 0;
                curY = j;
                while (while_break)
                {
                    if (curX == MAX-1)
                    {
                        //작은위치와 값기록
                        if (move_min > move)
                        {
                            move_min = move;
                            min_Y = startY;
                        }
                        while_break = 0;
                        move = 0;
                        continue;
                    }
 
                    if (from != EAST && ladder[curX][curY - 1== 1)
                    {
                        curY--;
                        from = WEST;
                        move++;
                        continue;
                    }
                    else if (from != WEST && ladder[curX][curY + 1== 1)
                    {
                        curY++;
                        from = EAST;
                        move++;
                        continue;
                    }
                    else if (from != SOUTH && ladder[curX + 1][curY] == 1)
                    {
                        curX++;
                        from = NORTH;
                        continue;
                    }
                }
            }
        }
        printf("#%d %d\n", test_case, min_Y);
    }//
    return 0;
}
 
cs


댓글