17142 연구소 3


1. 활성 바이러스를 m개 고르는 모든경우에 대해 탐색한다.

2. 선택된 활성바이러스를 퍼뜨린다.

3. 모든 영역을 바이러스로 만드는 최소시간을 찾는다.


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
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
 
struct Node {
    int r, c;
    Node(){}
    Node(int r, int c) :r(r), c(c){}
};
 
 
const int dr[] = { 0,0,1,-1 };
const int dc[] = { 1,-1,0,0 };
int n, m, res;
int map[51][51];
int tmp[51][51];
bool check[10];
vector<Node> v;
 
void copyMap() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (map[i][j] == 1) tmp[i][j] = -1;
            else tmp[i][j] = 0;
        }
    }
}
 
void printMap(int arr[][51]) {
    printf("\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}
 
int get_result() {
    
    copyMap();
 
    queue<Node> q;
    for (int i = 0; i < 10; i++) {
        if (check[i]) {
            tmp[v[i].r][v[i].c] = 1;
            q.push(Node(v[i].r, v[i].c));
        }
    }
 
    //바이러스 퍼뜨리기
    while (!q.empty()) {
        int r = q.front().r;
        int c = q.front().c;
        q.pop();
 
        for (int i = 0; i < 4; i++) {
            int ar = r + dr[i];
            int ac = c + dc[i];
 
            if (0 <= ar && 0 <= ac && ar < n&&ac < n) {
                if (tmp[ar][ac] == 0) {
                    q.push(Node(ar, ac));
                    tmp[ar][ac] = tmp[r][c] + 1;
                }
            }
        }
    }
 
    //printMap(tmp);
 
    //결과 확인
    int max_cnt = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (tmp[i][j] == -1continue;
            else if (tmp[i][j] == 0return 999;
            else if (map[i][j] == 2continue;
            
            max_cnt = max(max_cnt, tmp[i][j]);
        }
    }
 
    return max_cnt;
}
 
void dfs(int idx, int cnt) {
    check[idx] = true;
 
    if (cnt == m) {
        res = min(res, get_result());
        //printf("%d\n", res);
    }
    else {
        for (int i = idx + 1; i < v.size(); i++) {
            dfs(i, cnt + 1);
        }
    }
    
    check[idx] = false;
}
 
int main() {
    scanf("%d%d"&n, &m);
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d"&map[i][j]);
            if (map[i][j] == 2) {
                v.push_back(Node(i, j));
            }
        }
    }
 
    res = 987654321;
    for (int i = 0; i < v.size(); i++) {
        dfs(i, 1);
    }
 
    printf("%d\n", (res == 999) ? -1 : res - 1);
}
 
cs


'BOJ::문제풀이' 카테고리의 다른 글

17140 이차원배열과 연산  (0) 2019.04.17
16235 나무 재테크  (0) 2018.10.22
16236 아기상어  (0) 2018.10.22
16234 인구 이동  (0) 2018.10.22
15684 사다리 조작  (0) 2018.09.07

+ Recent posts