반응형

www.acmicpc.net/problem/16197

 

16197번: 두 동전

N×M 크기의 보드와 4개의 버튼으로 이루어진 게임이 있다. 보드는 1×1크기의 정사각형 칸으로 나누어져 있고, 각각의 칸은 비어있거나, 벽이다. 두 개의 빈 칸에는 동전이 하나씩 놓여져 있고,

www.acmicpc.net

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
#include <iostream>
#include <queue>
#define pii pair<intint>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, m;
char arr[21][21];
int dx[] = { 00-11 };
int dy[] = { -1100 };
bool visited[20][20][20][20];
queue<pii> q;
void bfs() {
    int cnt = 1;
    while (!q.empty()) {
        int size = q.size() / 2;    // 한 번에 두 개 꺼냄
        while (size--){
            if (cnt > 10)            // 10번 초과하면 불가
                return;
            pii c1 = q.front();
            q.pop();
            pii c2 = q.front();
            q.pop();
 
            if (c1 == c2)            // 두 동전이 겹치면 패스
                continue;
 
            rep(i, 4) {
                int nx1 = c1.first + dx[i];
                int ny1 = c1.second + dy[i];
                int nx2 = c2.first + dx[i];
                int ny2 = c2.second + dy[i];
 
                bool drop1 = false;
                bool drop2 = false;
                if (nx1 < 0 || nx1 >= n || ny1 < 0 || ny1 >= m)
                    drop1 = true;
                if (nx2 < 0 || nx2 >= n || ny2 < 0 || ny2 >= m)
                    drop2 = true;
 
                if (drop1 && drop2)    // 둘 다 떨어지면 패스
                    continue;
 
                else if ((drop1 && !drop2) || (!drop1 && drop2)) {    // 하나만 떨어짐
                    cout << cnt;
                    exit(0);
                }
 
                if (arr[nx1][ny1] == '#' && arr[nx2][ny2] == '#'continue// 둘 다 벽이면 패스
 
                if (arr[nx1][ny1] != '#' && arr[nx2][ny2] != '#') {            // 둘 다 벽 아니면 이동
                    if (!visited[nx1][ny1][nx2][ny2]) {
                        q.push({ nx1, ny1 });
                        q.push({ nx2, ny2 });
                        visited[nx1][ny1][nx2][ny2] = 1;
                    }
                }
                else if(arr[nx1][ny1] == '#' && arr[nx2][ny2] != '#') {        // 둘 중 하나가 벽이면
                    if (!visited[c1.first][c1.second][nx2][ny2]) {            
                        q.push(c1);                                            // 벽이면 제자리 삽입
                        q.push({ nx2, ny2 });                                // 벽 아니면 다음 위치 삽입
                        visited[c1.first][c1.second][nx2][ny2] = 1;
                    }
                }
                else if (arr[nx1][ny1] != '#' && arr[nx2][ny2] == '#') {
                    if (!visited[nx1][ny1][c2.first][c2.second]) {
                        q.push({ nx1, ny1 });
                        q.push(c2);
                        visited[nx1][ny1][c2.first][c2.second] = 1;
                    }
                }
                    
            }
        }
        cnt++;
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    rep(i, n) {
        rep(j, m) {
            cin >> arr[i][j];
            if (arr[i][j] == 'o'
                q.push({ i, j });
        }
    }
    if (q.size() == 1) {    // 입력된 동전 위치가 하나 -> 두 개의 동전이 겹침
        cout << -1;
        exit(0);
    }
    bfs();
    cout << -1;
}
cs
반응형

'백준 > DFS, BFS' 카테고리의 다른 글

백준 16929  (0) 2021.03.06
백준 16947 [복습 필수]  (0) 2021.03.06
백준 13549  (0) 2021.02.19
백준 14226 [복습 필수]  (0) 2021.02.19
백준 13913  (0) 2021.02.19

+ Recent posts