๋ฌธ์ ๋งํฌ
17142๋ฒ: ์ฐ๊ตฌ์ 3 (acmicpc.net)
17142๋ฒ: ์ฐ๊ตฌ์ 3
์ธ์ฒด์ ์น๋ช ์ ์ธ ๋ฐ์ด๋ฌ์ค๋ฅผ ์ฐ๊ตฌํ๋ ์ฐ๊ตฌ์์ ์น์์ด๊ฐ ์นจ์ ํ๊ณ , ๋ฐ์ด๋ฌ์ค๋ฅผ ์ ์ถํ๋ ค๊ณ ํ๋ค. ๋ฐ์ด๋ฌ์ค๋ ํ์ฑ ์ํ์ ๋นํ์ฑ ์ํ๊ฐ ์๋ค. ๊ฐ์ฅ ์ฒ์์ ๋ชจ๋ ๋ฐ์ด๋ฌ์ค๋ ๋นํ์ฑ ์ํ์ด๊ณ
www.acmicpc.net
์ฐ๊ตฌ์ ๋ชจ๋ ์นธ์ ๋ฐ์ด๋ฌ์ค๋ฅผ ํผํธ๋ฆฌ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ๊ตฌํ๋ BFS ๋ฌธ์ .
ํ์ด
์ฐ๊ตฌ์ค์ N X N ํฌ๊ธฐ์ ์นธ์ผ๋ก ํํํ์ ๋ ๊ฐ ์นธ์ 0 ๋น ์นธ, 1 ๋ฒฝ, 2 ๋ฐ์ด๋ฌ์ค ์ค์ ํ๋๋ก ์ฑ์์ง๋ค.
๋ฐ์ด๋ฌ์ค์๋ ํ์ฑ ๋ฐ์ด๋ฌ์ค์ ๋นํ์ฑ ๋ฐ์ด๋ฌ์ค๊ฐ ์๋ค. ๋๋ค ๋ฐ์ด๋ฌ์ค์ง๋ง ํ์ฑ ๋ฐ์ด๋ฌ์ค๋ ์ ์ผ์ฑ์ ๊ฐ์ง๊ณ ๋นํ์ฑ ๋ฐ์ด๋ฌ์ค๋ ์ ์ผ์ฑ์ด ์๋ค. ํ์ง๋ง ํ์ฑ ๋ฐ์ด๋ฌ์ค๊ฐ ๋นํ์ฑ ๋ฐ์ด๋ฌ์ค์ ๋ง๋๋ฉด ํ์ฑํ์ํฌ ์ ์๋ค.
2 ~ 10๊ฐ ์ฌ์ด์ ๋ฐ์ด๋ฌ์ค ์ค์ ํ์ฑ ๋ฐ์ด๋ฌ์ค๋ M๊ฐ ๊ฐ ์กด์ฌํ๋ค.
๋ฐ์ด๋ฌ์ค๋ค ์ค์ ์ด๋ค M๊ฐ์ ๋ฐ์ด๋ฌ์ค๋ฅผ ํ์ฑ ๋ฐ์ด๋ฌ์ค๋ก ๋ง๋ค์ด์ผ ๊ฐ์ฅ ๋น ๋ฅธ ์๊ฐ๋ด์ ์ฐ๊ตฌ์๋ฅผ ๋ฐ์ด๋ฌ์ค๋ก ๊ฐ๋ ์ฑ์ธ ์ ์์ ์ง ๊ตฌํ๋ ๋ฌธ์ ๋ค.
์ฌ๊ธฐ์ ์ด๋ป๊ฒ M๊ฐ๋ฅผ ์ ํํด์ผ์ง ๋น ๋ฅด๊ฒ ์ ์ผ์ํค๋๋ฐ ์ ๋ฆฌํ ์ง๋ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ด ์๋ค. ๋ฐ๋ผ์ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ค ํด๋ณด์์ผํ๋ค.
๋๋ ๋ฐฑํธ๋ํน์ ์ด์ฉํด์ ์ ํํ ํ์ฑ ๋ฐ์ด๋ฌ์ค๊ฐ M๊ฐ๊ฐ ๋๋ฉด BFS๋ฅผ ์ด์ฉํด์ ๋ชจ๋ ์นธ์ ๋ฐ์ด๋ฌ์ค๋ฅผ ํผํธ๋ฆด ์ ์๋์ง, ๊ฐ๋ฅํ๋ค๋ฉด ์๊ฐ์ด ์ผ๋ง๋ ๊ฑธ๋ฆฌ๋ ์ง ์ฒดํฌํด์ ์ต๋จ์๊ฐ์ ์ฐพ์๋ค.
for (int i = idx; i < virus.size(); i++) {
active_virus.push_back(virus[i]);
backtracking(depth + 1, i + 1);
active_virus.pop_back();
}
active_virus๋ ํ์ฑํ ์ํฌ ๋ฐ์ด๋ฌ์ค์ ์ขํ๋ฅผ ๋ชจ์๋์ ๋ฒกํฐ์ด๋ค. ์ด ๋ฒกํฐ์ ํฌ๊ธฐ๊ฐ M์ด ๋๋ฉด M๊ฐ๋ฅผ ์ ํํ๋ค๋ ๋ป์ด๋ค. ์์ ์ฝ๋๋ก M๊ฐ์ ๋ฐ์ด๋ฌ์ค๋ฅผ ์ ํํ ์ ์๋ค.
M๊ฐ๋ฅผ ๋ชจ๋ ์ ํํ๋ค๋ฉด ์ด์ BFS๋ก ํ์์ ์์ํด์ผํ๋ค. BFS๋ก ์ด๋ํ ์ ์๋ ์นธ์ ๋ฒฝ์ด ์๋ ์นธ๋ค ์ค ์ด์ ์ ๋ฐฉ๋ฌธํ์ง ์์๋ ์นธ๋ค๋ก ์ด๋ํ ์ ์์ผ๋ฉฐ ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ฉด ์๋๋ค.
Q. ๋ชจ๋ ์นธ์ ๋ฐ์ด๋ฌ์ค๊ฐ ํผ์ก๋์ง๋ ์ด๋ป๊ฒ ์ฒดํฌํ ๊น?
A. ์ด๊ธฐ ์ ๋ ฅ์์ ๋น ์นธ์ ์ ์ฆ 0์ ๊ฐ์๋ฅผ ์ ์ฅํด๋๊ณ , ํ์ํ๋ฉด์ ๋น ์นธ์ ๋ฐ์ด๋ฌ์ค๋ฅผ ํผํธ๋ฆฌ๊ฒ๋๋ฉด ์ด ๊ฐ์๋ฅผ ํ๋์ฉ ์ค์ฌ๋๊ฐ๋ค. ๋ง์ฝ ๋จ์ ๋น ์นธ์ ์๊ฐ ์์ผ๋ฉด ๋ชจ๋ ์นธ์ ๋ฐ์ด๋ฌ์ค๊ฐ ํผ์ก๋ค๋ ๋ป์ด๋ฏ๋ก, ์ด๋๊น์ง ๊ฑธ๋ฆฐ ์๊ฐ์ ์ฒดํฌํด์ ์ต์์๊ฐ์ ์ฐพ๋๋ค.
์ฒ์์๋ ์ ๋ฐฉ๋ฒ๋๋ก ํ์ง์์์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์๋ค. ์ฒ์์ ๋ฐฉ๋ฌธ๋์ง์์ ๋นํ์ฑ ๋ฐ์ด๋ฌ์ค๋ฅผ ๋ง๋๋ฉด ์ด ๋ฐ์ด๋ฌ์ค๋ฅผ ํ์ฑ์ํค๋ ๊ฒ์ผ๋ก ํ๋ค.
5 1
2 2 2 1 1
2 1 1 1 1
2 1 1 1 1
2 1 1 1 1
2 2 2 1 1
๊ทธ๋ ๊ฒ ํ๊ฒ ๋๋ฉด ๋ง์ง๋ง ์์ ์์ ์ด๋ฏธ ๋ชจ๋ ์นธ์ด ๋น ์นธ ์์ด ๋ฐ์ด๋ฌ์ค๋ก ์ฑ์์ก๋๋ค. ํ์ง๋ง ๋นํ์ฑ ๋ฐ์ด๋ฌ์ค๋ฅผ ๋ง๋๋ฉด ํ์ฑํ ์ํค๋ฏ๋ก ์ด๋์ ์๊ฐ์ด ๊ณ์ฐ๋๋ค. ๊ทธ๋์ 4๋ผ๋ ๊ฐ์ ์ถ๋ ฅํ๊ณ ์์๋ค.
๊ทธ๋ ๋ค๊ณ ๋นํ์ฑ ๋ฐ์ด๋ฌ์ค๋ฅผ ๋ง๋ฌ์ ๋ ๋ ์ด์ ํ์์ ํ์ง ์๋๋ค๋ฉด?
5 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
0 2 0 2 0
1 1 1 1 1
์์ ๋ฐ๋ก(์ง๋ฌธ๊ฒ์์์ ์ฐพ์์ต๋๋ค)์์ ๋ ๋ฐ์ด๋ฌ์ค ์ค ํ๋๋ฅผ ํ์ฑํ ์์ผฐ์ ๋ ๋ค๋ฅธ ๋ฐ์ด๋ฌ์ค๋ฅผ ๋ง๋๋ฉด ํ์์ ์ข ๋ฃํ๋ค. ๊ทธ๋ผ ๋ชจ๋ ์นธ์ ๋ฐ์ด๋ฌ์ค๋ฅผ ํผํธ๋ฆฌ์ง ๋ชปํ๊ฒ๋๋ค.
๊ทธ๋์ ์๊ฐํด๋ธ ๋ฐฉ๋ฒ์ด ๋น ์นธ์ ์๋ฅผ ์ ์ฅํด์ ๋ชจ๋ ์นธ์ด ๋ฐ์ด๋ฌ์ค๋ก ๊ฐ๋์ฐผ๋์ง ํ์ธํ๋ ๋ฐฉ๋ฒ์ด์๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 50 + 5;
int lab[MAX_N][MAX_N];
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
int N, M;
vector<pair<int, int>> virus;
vector<pair<int, int>> active_virus;
bool visited[MAX_N][MAX_N];
int ans = INF;
int Blank;
void backtracking(int depth, int idx);
bool is_ok(pair<int, int> position);
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
int blank = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> lab[i][j];
if (lab[i][j] == 2)
virus.push_back({ i,j });
else if (!lab[i][j])
Blank++;
}
}
backtracking(0, 0);
if (ans == INF)
cout << "-1\n";
else cout << ans << "\n";
return 0;
}
void backtracking(int depth, int idx) {
if (depth == M) {
//bfs ์์
queue<pair<pair<int, int>, int>> q;
for (int i = 0; i < active_virus.size(); i++)
q.push({ active_virus[i], 0 });
int Max_time = -1;
int remain_blank = Blank;
if (!remain_blank) {
ans = 0;
return;
}
memset(visited, 0, sizeof(visited));
while (!q.empty()) {
pair<int, int> now = q.front().first;
int Time = q.front().second;
q.pop();
if (visited[now.first][now.second])
continue;
visited[now.first][now.second] = true;
if (lab[now.first][now.second] == 0)
remain_blank--;
Max_time = max(Max_time, Time);
if (!remain_blank) {
ans = min(ans, Max_time);
return;
}
for (int i = 0; i < 4; i++) {
pair<int, int> next = { now.first + dy[i], now.second + dx[i] };
if (!is_ok(next))
continue;
q.push({ next, Time + 1 });
}
}
return;
}
else {
for (int i = idx; i < virus.size(); i++) {
active_virus.push_back(virus[i]);
backtracking(depth + 1, i + 1);
active_virus.pop_back();
}
}
}
bool is_ok(pair<int, int> position) {
if (position.first < 0 || position.first >= N || position.second < 0 || position.second >= N
|| visited[position.first][position.second] || lab[position.first][position.second] == 1)
return false;
return true;
}
|
cs |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 17822 : ์ํ ๋๋ฆฌ๊ธฐ (0) | 2022.05.18 |
---|---|
[BOJ] 17779 : ๊ฒ๋ฆฌ๋งจ๋๋ง 2 (0) | 2022.05.18 |
[BOJ] 11758 : CCW (0) | 2022.05.12 |
[BOJ] 17143 : ๋์์ (0) | 2022.05.11 |
[BOJ] 17144 : ๋ฏธ์ธ๋จผ์ง ์๋ ! (0) | 2022.05.11 |