알고리즘/백준

[백준 3055] 탈출 C++

겜도리도리 2021. 10. 21. 20:24
반응형

문제

백준 3055 탈출 C++

 

3055번: 탈출

사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제

www.acmicpc.net

풀이

고슴도치의 위치에서 bfs를 실행합니다.

time이 늘어날 때만, 물을 확산해줍니다.

소스 코드

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
#include <iostream>
#include <string>
#include <queue>
using namespace std;
 
char map[50][50];
int R, C;
bool visited[50][50];
// 물 확산 체크
bool TimeCheck[2500];
 
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
struct node
{
    int x;
    int y;
    int time;
};
 
// 물 확산
void SpreadWater() {
    // 물이 흐를 곳을 T로 바꿔줌
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) {
            if (map[i][j] == '*') {
                for (int k = 0; k < 4; k++) {
                    int nx = j + dx[k];
                    int ny = i + dy[k];
                    if (nx >= 0 && ny >= 0 && nx < C && ny < R && map[ny][nx] == '.') {
                        map[ny][nx] = 'T';
                    }
                }
            }
        }
    }
    // T를 물로 바꿔줌
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) {
            if (map[i][j] == 'T') {
                map[i][j] = '*';
            }
        }
    }
}
 
int bfs(int sx, int sy) {
    visited[sy][sx] = true;
    queue<node> q;
    q.push({ sx, sy, 0 });
    while (!q.empty()) {
        int x = q.front().x;
        int y = q.front().y;
        int time = q.front().time;
        // 시작 위치 바꿔줌
        map[y][x] = 'S';
        // 해당 time에 확산된 적 없을 때만 물 확산
        if (!TimeCheck[time])
        {
            SpreadWater();
            TimeCheck[time] = true;
        }
        q.pop();
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && ny >= 0 && nx < C && ny < R && !visited[ny][nx]) {
                // 비버 굴이면 time 리턴
                if (map[ny][nx] == 'D')
                    return time + 1;
                // 빈 공간이면 이동
                else if (map[ny][nx] == '.')
                {
                    visited[ny][nx] = true;
                    q.push({ nx, ny, time + 1 });
                }
            }
        }
        // 시작 위치 원래대로 돌림
        map[y][x] = '.';
    }
    return -1;
}
 
int main()
{
    // 입력
    cin >> R >> C;
    string s;
    int sx, sy;
    for (int i = 0; i < R; i++) {
        cin >> s;
        for (int j = 0; j < s.size(); j++) {
            map[i][j] = s[j];
            if (s[j] == 'S')
            {
                sx = j;
                sy = i;
            }
        }
    }
 
    int ans = bfs(sx, sy);
    // 도달할 수 없으면 KAKTUS 출력
    if (ans == -1) {
        cout << "KAKTUS";
    }
    else {
        cout << ans;
    }
    return 0;
}
cs
반응형

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

[백준 1707] 이분 그래프 C++  (0) 2021.10.26
[백준 1806] 부분합 C++  (0) 2021.10.25
[백준 1197] 최소 스패닝 트리 C++  (0) 2021.10.20
[백준 1520] 내리막 길 C++  (0) 2021.10.17
[백준 14499] 주사위 굴리기 C++  (0) 2021.10.13