본문 바로가기

시뮬레이션과 구현

연구소 3

https://www.acmicpc.net/problem/17142

 

17142번: 연구소 3

인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 바이러스는 활성 상태와 비활성 상태가 있다. 가장 처음에 모든 바이러스는 비활성 상태이고,

www.acmicpc.net

 


조합을 이용해서 활성 바이러스를 고르고
최소 시간이니까 BFS를 하자.
jikan을 visited로 쓰면 되겠다.


초기에 입력을 받을 때 빈 공간의 갯수를 Count해주었다.

탐색을 진행하면서, 빈 공간을 탐색할 때 마다 그 개수를 Count해줄 것이고,

이 개수와 처음에 Count해놓은 빈 공간의 개수와 비교해서 모두 탐색이 가능한지 확인

 

추가 조건

활성 바이러스가 비활성 바이러스가 있는 칸으로 가면

비활성 바이러스가 활성으로 변한다.

비활성 바이러스를 만나면 시간 추가를 안한다....

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
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
 
using namespace std;
 
int dx[] = { 00-11 };
int dy[] = { 1-100 };
 
int n, m;
int aki;
int ans = 987654321;
 
vector<pair<intint>> virus;
int map[50][50];
//visited의 기능까지 한다!
int jikan[50][50];
bool chk[15];
 
void bfs(queue<pair<int,int>> q)
{
    int infect = 0;
    // 0을 갈 때만 total 갱신
    int total = 0;
    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
 
        for (int k = 0; k < 4; k++)
        {
            int nx = x + dx[k];
            int ny = y + dy[k];
 
            if (nx >= 0 && nx < n && ny >= 0 && ny < n)
            {
                if (map[nx][ny] != 1 && jikan[nx][ny] == -1)
                {
                    jikan[nx][ny] = jikan[x][y] + 1;
                    if (map[nx][ny] == 0)
                    {
                        infect++;
                        total = jikan[nx][ny];
                    }
                    q.push(make_pair(nx, ny));
                }
            }
        }
    }
    if (infect == aki)
    {
        if (ans > total)
            ans = total;
    }
 
}
 
void go(int index, int cnt)
{
    if (cnt == m)
    {
        queue<pair<intint>> q;
        memset(jikan, -1sizeof(jikan));
        for (int i = 0; i < virus.size(); i++)
        {
            if (chk[i])
            {
                q.push(make_pair(virus[i].first, virus[i].second));
                jikan[virus[i].first][virus[i].second] = 0;
            }
        }
 
        bfs(q);
        return;
    }
 
    for (int i = index; i < virus.size(); i++)
    {
        if (chk[i])
            continue;
        chk[i] = true;
        go(i + 1, cnt + 1);
        chk[i] = false;
    }
}
 
int main()
{
    ios::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 >> map[i][j];
            if (map[i][j] == 0)
                aki++;
            else if (map[i][j] == 2)
            {
                virus.push_back({ i, j });
            }
        }
    }
 
    go(00);
 
    if (ans == 987654321)
        cout << -1 << '\n';
    else
        cout << ans << '\n';
 
    return 0;
}
cs

'시뮬레이션과 구현' 카테고리의 다른 글

이차원 배열과 연산  (0) 2023.09.26
게리맨더링 2  (0) 2023.09.25
아기 상어  (0) 2023.09.23
마법사 상어와 파이어볼 + 비바라기  (1) 2023.09.21
배열 돌리기 4  (1) 2023.09.18