ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 사다리 조작
    백준알고리즘 2019. 9. 21. 02:34

    https://www.acmicpc.net/problem/15684

     

    15684번: 사다리 조작

    사다리 게임은 N개의 세로선과 M개의 가로선으로 이루어져 있다. 인접한 세로선 사이에는 가로선을 놓을 수 있는데, 각각의 세로선마다 가로선을 놓을 수 있는 위치의 개수는 H이고, 모든 세로선이 같은 위치를 갖는다. 아래 그림은 N = 5, H = 6 인 경우의 그림이고, 가로선은 없다. 초록선은 세로선을 나타내고, 초록선과 점선이 교차하는 점은 가로선을 놓을 수 있는 점이다. 가로선은 인접한 두 세로선을 연결해야 한다. 단, 두 가로선이 연속하거나 서로

    www.acmicpc.net

    삼성 S직군 역량평가 기출문제다.

    일단 문제파악 순서는

    사다리를 놓을수있고, 놓았을때 오른쪽으로갈수있는 부분을 1, 왼쪽으로 갈수있는 부분을 2로표시했고

    사다리를 내려가서 확인하는 과정을 game()함수로 생성했으며 함수 안에서 i번째 자리가 , i 출구로 나오는지 확인하고

    아닐시 다시 확인하는 방식으로 진행함

    우선, 이문제는 배열의 크기가 커봐야 30 * 10 = 300 배열에 조합의크기가 300C3정도의 크기인데 이것보다 작기때문에

    모든경우의수를 해봐도 괜찮을것같다는 생각으로 순열을 사용한 조합으로 문제를 풀었다.

    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
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    #include<iostream>
    #include<vector>
    #include<utility>
    #include<algorithm>
    using namespace std;
     
    int n, m, h;
    int map[32][11];
    int temp[32][11];
    vector<pair<intint>>v;
    vector<int> per;
    int ans;
    bool ending = false;
     
    void game(vector<int>&a, int cnt){        
        do {
            bool ongame = true;
            for (int i = 1; i <= h; i++) {
                for (int j = 1; j <= n; j++) {
                    temp[i][j] = map[i][j];
                }
            }
            for (int i = 0; i < a.size(); i++) {
                if (a[i] == 1) {
                    if (temp[v[i].first][v[i].second] == 0 && 
                        temp[v[i].first][v[i].second + 1]== 0 &&
                        v[i].second + 1 <= n) {
                        
                        temp[v[i].first][v[i].second] = 1;
                        temp[v[i].first][v[i].second + 1= 2;
                        
                    }
                    else {
                        ongame = false;
                    }
                }
            }
            
            if (ongame) {
                int cox = 0;
                for (int i = 1; i <= n; i++) {
                    int x = 1int y = i; int idx = i;
                    bool endloop = false;
                    while (!endloop) {
                        if (temp[x][y] == 0) {
                            x++;
                        }
                        else if (temp[x][y] == 1) {
                            y++;
                            x++;
                        }
                        else if (temp[x][y] == 2) {
                            y--;
                            x++;
                        }
     
                        if (x == h + 1) {
                            if (y == idx) {
                                cox++;
                            }
                            else {
                                i = n + 1;
                            }
                            endloop = true;
                        }
                    }
                }
                if (cox == n) {
                    ending = true;
                }
            }
        } while (prev_permutation(a.begin(), a.end()));
     
    }
     
     
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
     
        cin >> n >> m >> h;
        for (int i = 0; i < m; i++) {
            int H, N;
            cin >> H >> N;
            map[H][N] = 1;
            map[H][N + 1= 2;
        }
        
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j < n; j++) {
                if (map[i][j] == 0) {
                    v.push_back({ i,j });
                }
            }
        }
        vector<int> per[4];
        for (int i = 0; i < v.size(); i++) {
            if (i < 1) {
                per[1].push_back(1);
            }
            else per[1].push_back(0);
            if (i < 2)
                per[2].push_back(1);
            else
                per[2].push_back(0);
            if (i < 3)
                per[3].push_back(1);
            else
                per[3].push_back(0);
            per[0].push_back(0);
        }
        
        for (int i = 0; i < 4; i++) {
            game(per[i], i);
            if (ending == true) { cout << i << '\n'return 0; }
        }
        cout << -1 << '\n';
        return 0;
     
    }
    http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
     

    코드는 다음과같으며 채점결과를보니 1290ms정도 나왔는데 가로 배열이 한두줄만 더길었어도 실패했을것같다

     

    순열조합 사용하는 방식은 조금 지양하고 재귀 쓰는것에 익숙해지자.

    '백준알고리즘' 카테고리의 다른 글

    촌수계산  (0) 2019.09.21
    신기한 소수  (0) 2019.09.21
    게리맨더링  (0) 2019.09.20
    상범 빌딩  (0) 2019.09.17
    역사  (0) 2019.09.17

    댓글

Designed by Tistory.