문제 링크 : https://www.acmicpc.net/problem/13460



삼성 기출문제이다.


구현하는게 좀 짜증나는 문제였다.


dfs + 시뮬레이션으로 풀었다.


갈수있는 경우를 깊이우선으로 탐색하여 파란 구슬을 탈출하지 않고 빨간 구슬만 탈출하는 경우, 몇번만에 탈출하였는지 벡터에 저장 후 전체탐색이 끝나면 벡터의 값 중 가장 작은 값을 출력하여주었다.



아래는 소스코드이다.


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
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;
 
int m, n;
char map[10][10];
//상오하왼
int dy[] = { -1010 };
int dx[] = { 010-1 };
 
vector<int> v;
 
// 정상졸료 0, 탈출 1
int move(int drt, int& ny, int& nx, int othy, int othx) {
    bool enemy = false;
    if (drt == 0) {
        //위로 이동
        if (othx == nx && othy < ny) {
            enemy = true;
        }
        for (int i = ny - 1; i > 0; i--) {
            if (enemy) {
                if (i == othy) break;
            }
            if (map[i][nx] != '.') {
                break;
            }
            ny--;
        }
        if (map[ny - 1][nx] == 'O') {
            ny = -1;
            nx = -1;
            return 1;
        }
        else
            return 0;
    }
    else if (drt == 1) {
        //오른쪽으로 이동
        if (othy == ny && othx > nx) {
            enemy = true;
        }
        for (int i = nx + 1; i < m - 1; i++) {
            if (enemy) {
                if (i == othx) break;
            }
            if (map[ny][i] != '.') {
                break;
            }
            nx++;
        }
        if (map[ny][nx + 1== 'O') {
            ny = -1;
            nx = -1;
            return 1;
        }
        else
            return 0;
    }
    else if (drt == 2) {
        //아래로 이동
        if (othx == nx && othy > ny) {
            enemy = true;
        }
        for (int i = ny + 1; i < n - 1; i++) {
            if (enemy) {
                if (i == othy) break;
            }
            if (map[i][nx] != '.') {
                break;
            }
            ny++;
        }
        if (map[ny + 1][nx] == 'O') {
            ny = -1;
            nx = -1;
            return 1;
        }
        else
            return 0;
    }
    else if (drt == 3) {
        //왼쪽으로 이동
        if (othy == ny && othx < nx) {
            enemy = true;
        }
        for (int i = nx - 1; i > 0; i--) {
            if (enemy) {
                if (i == othx) break;
            }
            if (map[ny][i] != '.') {
                break;
            }
            nx--;
        }
        if (map[ny][nx - 1== 'O') {
            ny = -1;
            nx = -1;
            return 1;
        }
        else
            return 0;
    }
    return 3;
}
 
void go(int ry, int rx, int by, int bx, int direction, int cnt) {
    if (cnt == 10return;
 
    for (int d = 0; d < 4; d++) {
        int wry = ry;
        int wrx = rx;
        int wby = by;
        int wbx = bx;
 
        if (direction != -1 && (direction + 2) % 4 == d) continue;
        int nry = ry + dy[d];
        int nrx = rx + dx[d];
        bool rvs = false;
        
        if (d == 0) {
            if (rx == bx && by < ry) rvs = true;
        }
        else if (d == 1) {
            if (ry == by && bx > rx) rvs = true;
        }
        else if (d == 2) {
            if (rx == bx && by > ry) rvs = true;
        }
        else if (d == 3) {
            if (ry == by && bx < rx) rvs = true;
        }
 
        if (rvs) {
            //b움직, r움직, 탈출인지 확인, 재귀
            int br = move(d, wby, wbx, wry, wrx);
            int rr = move(d, wry, wrx, wby, wbx);
            if (br == 1continue;
            if (br == 0 && rr == 1) {
                v.push_back(cnt + 1);
                continue;
            }
            go(wry, wrx, wby, wbx, d, cnt + 1);
        }
        else {
            //r움직, b움직, 탈출인지 확인, 재귀
            int rr = move(d, wry, wrx, wby, wbx);
            int br = move(d, wby, wbx, wry, wrx);
            if (br == 1continue;
            if (br == 0 && rr == 1) {
                v.push_back(cnt + 1);
                continue;
            }
            go(wry, wrx, wby, wbx, d, cnt + 1);
        }
    }
 
}
 
 
int main()
{
    int fRy, fRx, fBy, fBx;
    char t;
    cin >> n >> m;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> t;
            if (t == 'R') {
                fRy = i;
                fRx = j;
                map[i][j] = '.';
            }
            else if (t == 'B') {
                fBy = i;
                fBx = j;
                map[i][j] = '.';
            }
            else {
                map[i][j] = t;
            }
        }
    }
 
    go(fRy, fRx, fBy, fBx, -10);
 
    if (v.size() == 0cout << -1;
    else {
        int minr = 9999;
        for (int i = 0; i < v.size(); i++) {
            minr = min(minr, v[i]);
        }
        cout << minr;
    }
}
cs



'Algorithm > 백준' 카테고리의 다른 글

백준 14888번 연산자 끼워넣기  (0) 2019.04.27
백준 14500번 테트로미노  (0) 2019.04.26
백준 2407 조합  (0) 2019.02.26
백준 10974 모든 순열  (0) 2019.02.25
백준 1764 듣보잡  (0) 2019.02.20

+ Recent posts