본문 바로가기

알고리즘 문제풀이

[Baekjoon] 백준 구슬 탈출 2(13460번) 문제풀이(C++)

반응형

핵심 : 

  1. 재귀를 통해 모든 경우의 수 탐색 + 중복 방지

  2. 기울이기 동작 구현

더 짧게 구현할 수 있을 듯 한데 당시 구현은 이렇게 했습니다!

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include<iostream>
#include<vector>
#include<algorithm>
 
#define MAXX 987654321
using namespace std;
/*
<문제>
 
직사각형 보드에 빨강, 파랑 구슬 하나씩 넣고 
빨간 구슬을 구멍을 통해 빼내는 게임
 
보드 세로 N, 가로 M
가장 바깥 행, 열은 모두 막힘
구멍 하나 있음 -> 여기로 빨간 구슬 빼기 / 파란 구슬은 안됨
 
중력을 통해 굴리기(상하좌우 동작 가능)
기울이기를 통해 굴리고, 구슬이 움직이지 않을 때 까지 기울임
 
보드 상태가 주어졌을 때, 최소 몇 번 만에 빨간 구슬을 뺄 수 있는지 구하기
 
 
입력:
N M (M 10 이하)
N개 줄
    보드 모양을 나타내는 문자열(길이 M) -> . # O R B 중 하나
 
출력:
최소 몇 번 만에 빨간 구슬을 구멍으로 빼낼 수 있는 지
(10번 이하로 못 빼내면 -1 출력)
 
 
<풀이>
 
재귀?
직접 다 해보면서, 최소값 뽑기
기울이면 안되는 경우 제거
 
기울이면 안되는 경우(하나라도 해당되면 종료)
    1. 파란 구슬이 구멍에 감
    2. 파랑, 빨강 구슬 움직임 변동이 없음
    3. 총 횟수가 10번 넘어감
 
끝나는 경우(전부 해당해야 함)
    1. 빨강이 구멍에 도착
    2. 파랑은 구멍이 아닌 곳에 도착
    3. 총 횟수가 10번 이하
    
 
<시간>
O(4^10) -> 1048576번
*/
 
 
//현 상황을 담음
struct Situation {
    int cnt;
    pair<intint> red;
    pair<intint> blue;
 
    Situation(pair<intint> redt, pair<intint> bluet) {
        //cnt = cntt;
        red = redt;
        blue = bluet;
    }
};
 
//한 번 기울임 -> 1, 2, 3, 4 순으로 상하좌우
//red/blue : 공 위치
Situation MoveOne(vector<string> pan, int way, pair<intint> red, pair<intint> blue) {
 
    int rx = red.first, ry = red.second, bx = blue.first, by = blue.second;
    bool is_hole = false;    //구멍 도착 여부
 
    if (way == 1) {    //상
 
        while (rx >= 0 && pan[rx][ry] == '.') { rx--; } rx++;
        while (bx >= 0 && pan[bx][by] == '.') { bx--; } bx++;
 
    
        if (rx != 0 && pan[rx - 1][ry] == 'O') { is_hole = true; rx--; }
        if (bx != 0 && pan[bx - 1][by] == 'O') { is_hole = true; bx--; }
 
        if (rx == bx && ry == by && red.first < blue.first && !is_hole) bx++;
        else if (rx == bx && ry == by && red.first > blue.first && !is_hole) rx++;
    }
    else if (way == 2) {    //하
        
        while (rx <= pan.size() && pan[rx][ry] == '.') { rx++; } rx--;
        while (bx <= pan.size() && pan[bx][by] == '.') { bx++; } bx--;
 
        if (rx != pan.size() - 1 && pan[rx + 1][ry] == 'O') { is_hole = true; rx++; }
        if (bx != pan.size() - 1 && pan[bx + 1][by] == 'O') { is_hole = true; bx++; }
 
        if (rx == bx && ry == by && red.first < blue.first && !is_hole) rx--;
        else if (rx == bx && ry == by && red.first > blue.first && !is_hole) bx--;
    }
    else if (way == 3) {    //좌
 
        while (ry >= 0 && pan[rx][ry] == '.') { ry--; } ry++;
        while (by >= 0 && pan[bx][by] == '.') { by--; } by++;
 
        if (ry != 0 && pan[rx][ry - 1== 'O') { is_hole = true; ry--; }
        if (by != 0 && pan[bx][by - 1== 'O') { is_hole = true; by--; }
 
        if (rx == bx && ry == by && red.second < blue.second && !is_hole) by++;
        else if (rx == bx && ry == by && red.second > blue.second && !is_hole) ry++;
    }
    else if (way == 4) {    //우
 
        while (ry <= pan[0].size() && pan[rx][ry] == '.') { ry++; } ry--;
        while (by <= pan[0].size() && pan[bx][by] == '.') { by++; } by--;
 
        if (ry != pan[0].size() && pan[rx][ry + 1== 'O') { is_hole = true; ry++; }
        if (by != pan[0].size() && pan[bx][by + 1== 'O') { is_hole = true; by++; }
 
        if (rx == bx && ry == by && red.second < blue.second && !is_hole) ry--;
        else if (rx == bx && ry == by && red.second > blue.second && !is_hole) by--;
    }
 
    red.first = rx, red.second = ry, blue.first = bx, blue.second = by;
    return Situation(red, blue);
 
}
 
 
int reverse(int a) {    //a의 반대방향 return
    if (a == 1return 2;
    else if (a == 2return 1;
    else if (a == 3return 4;
    else if (a == 4return 3;
    else return 0;
}
 
int minn = MAXX;
 
//pan: 보드 현황, cnt : 기울임 카운트, red/blue : 각 공 위치, prev: 이전에 기울인 방향
int getAns(vector<string> pan, int cnt, pair<intint>red, pair<intint>blue, int prev) {    //기울이기 시뮬레이션
 
    //기울이면 안되는 경우
    if (pan[blue.first][blue.second] == 'O'return MAXX;
    if (cnt > 10return MAXX;
 
    //끝나는 경우
    if (pan[red.first][red.second] == 'O' && pan[blue.first][blue.second] != 'O')
        return cnt;
 
    for (int i = 1; i <= 4; i++) {    //상하좌우 기울인 후, 판단
        
        //이건 이동만
        if (i == reverse(prev)) continue;    //이전 방향의 반대방향이면 건너뛰기
        Situation tmp = MoveOne(pan, i, red, blue);    //red/blue 공 위치, cnt만 반환(pan은 어차피 같음)
 
        int res;
 
        if (tmp.red == red && tmp.blue == blue) res = MAXX;    //움직임 변동이 없으면 끝
        else res = getAns(pan, cnt + 1, tmp.red, tmp.blue, i);
 
        if (res < minn) minn = res;
    }
 
    if (minn == MAXX) return MAXX;
    else return minn;
}
 
int main() {
 
    int n, m; cin >> n >> m;
    //n, m 입력
 
    vector<string> pan;    //보드판
 
    //각 공 위치
    pair<intint> red;    
    pair<intint> blue;
 
 
    for (int i = 0; i < n; i++) {    //n개의 줄
        string s; cin >> s;    //문자열 입력
 
        //공 위치 설정
        for (int j = 0; j < s.size(); j++) {
            if (s[j] == 'R') {
                red.first = i; red.second = j; s[j] = '.';
            }
            if (s[j] == 'B') {
                blue.first = i; blue.second = j; s[j] = '.';
            }
        }
 
        pan.push_back(s);
    }
 
    int ans = getAns(pan, 0, red, blue, 0);
    if (ans == MAXX) ans = -1;
    cout << ans;
 
    return 0;
}
cs

반응형