forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3.cpp
More file actions
80 lines (70 loc) ยท 2.33 KB
/
3.cpp
File metadata and controls
80 lines (70 loc) ยท 2.33 KB
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
#include <bits/stdc++.h>
using namespace std;
class Virus {
public:
int index;
int second;
int x;
int y;
Virus(int index, int second, int x, int y) {
this->index = index;
this->second = second;
this->x = x;
this->y = y;
}
// ์ ๋ ฌ ๊ธฐ์ค์ '๋ฒํธ๊ฐ ๋ฎ์ ์์'
bool operator <(Virus &other) {
return this->index < other.index;
}
};
int n, k;
// ์ ์ฒด ๋ณด๋ ์ ๋ณด๋ฅผ ๋ด๋ ๋ฐฐ์ด
int graph[200][200];
// ๋ฐ์ด๋ฌ์ค์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๋ ๋ฆฌ์คํธ
vector<Virus> viruses;
// ๋ฐ์ด๋ฌ์ค๊ฐ ํผ์ ธ๋๊ฐ ์ ์๋ 4๊ฐ์ง์ ์์น
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main(void) {
cin >> n >> k;
// ๋ณด๋ ์ ๋ณด๋ฅผ ํ ์ค ๋จ์๋ก ์
๋ ฅ
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> graph[i][j];
// ํด๋น ์์น์ ๋ฐ์ด๋ฌ์ค๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ
if (graph[i][j] != 0) {
// (๋ฐ์ด๋ฌ์ค ์ข
๋ฅ, ์๊ฐ, ์์น X, ์์น Y) ์ฝ์
viruses.push_back(Virus(graph[i][j], 0, i, j));
}
}
}
// ์ ๋ ฌ ์ดํ์ ํ๋ก ์ฎ๊ธฐ๊ธฐ (๋ฎ์ ๋ฒํธ์ ๋ฐ์ด๋ฌ์ค๊ฐ ๋จผ์ ์ฆ์ํ๋ฏ๋ก)
sort(viruses.begin(), viruses.end());
queue<Virus> q;
for (int i = 0; i < viruses.size(); i++) {
q.push(viruses[i]);
}
int target_s, target_x, target_y;
cin >> target_s >> target_x >> target_y;
// ๋๋น ์ฐ์ ํ์(BFS) ์งํ
while (!q.empty()) {
Virus virus = q.front();
q.pop();
// ์ ํํ second๋งํผ ์ด๊ฐ ์ง๋๊ฑฐ๋, ํ๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต
if (virus.second == target_s) break;
// ํ์ฌ ๋
ธ๋์์ ์ฃผ๋ณ 4๊ฐ์ง ์์น๋ฅผ ๊ฐ๊ฐ ํ์ธ
for (int i = 0; i < 4; i++) {
int nx = virus.x + dx[i];
int ny = virus.y + dy[i];
// ํด๋น ์์น๋ก ์ด๋ํ ์ ์๋ ๊ฒฝ์ฐ
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
// ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์์น๋ผ๋ฉด, ๊ทธ ์์น์ ๋ฐ์ด๋ฌ์ค ๋ฃ๊ธฐ
if (graph[nx][ny] == 0) {
graph[nx][ny] = virus.index;
q.push(Virus(virus.index, virus.second + 1, nx, ny));
}
}
}
}
cout << graph[target_x - 1][target_y - 1] << '\n';
}