-
Notifications
You must be signed in to change notification settings - Fork 1
/
Competitive_Contagion.cpp
71 lines (54 loc) · 1.07 KB
/
Competitive_Contagion.cpp
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
#include<bits/stdc++.h>
using namespace std;
int dx[4] = { 0, -1, 0, 1 };
int dy[4] = { 1, 0, -1, 0 };
int field[200][200];
struct virus
{
int y, x, virus, day;
// 날짜순, 바이러스 번호 순으로 정렬
bool operator>(const struct virus& v) const
{
if (day == v.day)
return virus > v.virus;
return day > v.day;
}
};
priority_queue<virus, vector<virus>, greater<virus>> q;
int main()
{
int n, k, x, y, s;
cin >> n >> k;
for (int y = 0; y < n; y++)
{
for (int x = 0; x < n; x++)
{
int v;
cin >> v;
field[y][x] = v;
if (v > 0)
{
q.push({ y,x,v,1 });
}
}
}
cin >> s >> x >> y;
// BFS
while (!q.empty())
{
// s day가 지나면 전염을 멈춘다
if (q.top().day > s) break;
virus front = q.top();
q.pop();
// 사방으로 인접한 요소들을 전염시킨다
for (int i = 0; i < 4; i++)
{
int nx = front.x + dx[i];
int ny = front.y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || field[ny][nx] > 0) continue;
field[ny][nx] = front.virus;
q.push({ ny, nx, front.virus, front.day + 1 });
}
}
cout << field[x - 1][y - 1];
}