반응형

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

 

17141번: 연구소 2

인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 M개를 놓을 것이고, 승원이의 신호와 동시에 바이

www.acmicpc.net

<문제 풀이> 조합, BFS, 시뮬레이션

선택 가능한 바이러스 중에서 M개를 뽑는 조합을 구현하고 BFS로 바이러스를 퍼뜨리면 됩니다.

이때  2차원 배열의 각 원소의 인덱스를 0, 1, 2, 3... (N * N)이라고 생각하면 1차원 배열에서의 조합과 마찬가지로 조합 알고리즘을 적용할 수 있습니다.

그리고 이 인덱스를 이용해서 2차원 배열의 좌표를 다음과 같이 구할 수 있습니다.

x = i % N
y = i / N 

 

 

<C++ 소스 코드>

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
#include<iostream>
#include<queue>
#include<utility>
#include<algorithm>
using namespace std;
 
#define X first
#define Y second
#define SELECTABLE 2 //바이러스로 뽑을 수 있는 경우
#define VIRUS -1 // 바이러스로 뽑은 경우
#define WALL 1 // 벽
#define EMPTY 0 //빈 칸
const int dx[4= { 001-1 };
const int dy[4= { 1-100 };
int N, M;
int board[50][50];
int visited[50][50];
int res = -1;
 
 
void dfs(int idx, int cnt) {
    if (cnt == M) {  //바이러스를 M개 뽑으면 
        fill(&visited[0][0], &visited[49][50], -1);
        queue<pair<intint> > Q;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (board[i][j] == VIRUS) {
                    Q.push({ i, j });
                    visited[i][j] = 0;
                }
            }
        }
        //바이러스 퍼뜨리기
        while (!Q.empty()) {
            auto cur = Q.front(); Q.pop();
            for (int dir = 0; dir < 4; dir++) {
                int nx = cur.X + dx[dir];
                int ny = cur.Y + dy[dir];
                if (nx < 0 || ny < 0 || nx >= N || ny >= N)continue;
                if (board[nx][ny] == WALL || board[nx][ny] == VIRUS) continue;
                Q.push({ nx, ny });
                board[nx][ny] = VIRUS;
                visited[nx][ny] = visited[cur.X][cur.Y] + 1;
                
            }
        }
        int tempRes = -1;
        //바이러스를 모두 퍼트렸을 때
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (board[i][j] == WALL) continue//벽은 무시
                if (board[i][j] == EMPTY) return//하나라도 퍼트리지 못했으면 -1
                tempRes = max(tempRes, visited[i][j]); // 퍼진 시간 갱신
            }
 
        }
        if (res == -1) res = tempRes; // 처음 갱신
        else res = min(res, tempRes); //가장 최소인 시간
        return;
    }
    int temp[50][50];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++){
            temp[i][j] = board[i][j];
        }
    }
    for (int i = idx; i < N * N; i++) {
        int x = i / N;
        int y = i % N;
        if (board[x][y] == SELECTABLE) { //바이러스로 선택 가능하면
            board[x][y] = VIRUS; // 바이러스로 선택
            dfs(i + 1, cnt + 1);
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++)
                    board[i][j] = temp[i][j];
            }
        }
 
    }
}
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
 
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> board[i][j];
        }
    }
 
    dfs(00);
 
    cout << res;
    
 
    return 0;
}
cs

 

반응형

'알고리즘 문제풀이 > 백준' 카테고리의 다른 글

[백준 1071번] 소트  (0) 2023.02.25
[백준 19238번] 스타트 택시  (0) 2022.09.04
[백준 17144번] 미세먼지 안녕!  (0) 2022.08.03
[백준 1026번] 보물  (0) 2022.08.01
[백준 1932번] 정수 삼각형  (0) 2022.05.06

+ Recent posts