๋ฌธ์ ๋งํฌ
6716๋ฒ: Collecting Beepers (acmicpc.net)
๋ฐฑํธ๋ํน์ด๋ผ๊ณ ์๊ฐ๋์ด์์ด์ ํ์ด๋ณธ ๋ฌธ์ ์ง๋ง ๋ฑํ ๋ฐฑํธ๋ํน์ผ๋ก ํ๋ฆฌ๋ ๋ฌธ์ ๊ฐ ์๋๋ผ์ ๋ฐฑํธ๋ํน ๋ฌธ์ ๋ก๋ ์ถ์ฒ ์ํจ.
ํ์ด
Karel์ ๊ฒฝ๋ก๋ฅผ ๋ฐฑํธ๋ํน์ผ๋ก ์ค์ ํด์ฃผ๋ ๋ฌธ์ ์ธ๊ฐ๋ณด๋ค ํ๊ณ ํ๊ธฐ ์์ํ๋ค. ํ์ง๋ง ์ด๋ฅผ ๋ฐฑํธ๋ํน์ผ๋ก ๊ตฌํํ๊ธฐ์ ๋๋ฌด ๋ณต์กํด ๋ณด์๋ค.
๊ทธ๋์ ๊ณ์ ์๊ฐํ๋ค๊ฐ ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉํด์ ํ์์น์์ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ณณ๋ถํฐ ๊ณ์ํด์ ์ด๋ํ๋ฉด ์ด๋จ๊น ํ๊ณ ํ์ด๋ณด์๋๋ ์์ ๊ฐ ๋ง์๋ค. ๊ธฐ๋ถ์ข๊ฒ ์ ์ถํ์ง๋ง 50%์์ ํ๋ ธ์ต๋๋ค๊ฐ ๋์๋ค. ์ด ๋ฐฉ๋ฒ์ด ํ๋ฆฐ ๊ฒ๊ฐ์ ๋ฐฑํธ๋ํน์ผ๋ก ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์๊ฐํด๋ดค์ง๋ง ๋ ์ค๋ฅด์ง ์์๋ค. ๋ฌธ์ ๋ฅผ ํผ ์ฌ๋์ด ์ ์ด ๊ตฌ๊ธ๋งํด์ ๊ฒจ์ฐ ๋ต์ ์ฐพ์ ์ ์์๋ค. ์ ์กฐ์ ์ฝ๋๋ฅผ ์ฐธ์กฐํ๋๋ฐ next_permutation ์ ์ฌ์ฉํด์ ํ์๋ค. next_permutation์ ์์ด ์ฆ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์์๋๋ก ๋ค ํด๋ณด๋ ๊ฒ์ด๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAX_N = 40000 + 5;
int N, T;
int st_y, st_x;
int MAX_X, MAX_Y;
vector<pair<int,int>> beeper;
int visited[10];
int get_distance() {
int sum = 0;
int now_x = st_x, now_y = st_y;
for (int i = 0; i < beeper.size(); i++) {
sum += abs(now_x - beeper[i].first) + abs(now_y - beeper[i].second);
now_x = beeper[i].first;
now_y = beeper[i].second;
}
sum += abs(now_x - st_x) + abs(now_y - st_y);
return sum;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
cin >> MAX_X >> MAX_Y;
cin >> st_x >> st_y;
cin >> N;
memset(visited, 0, sizeof(visited));
beeper.clear();
int x, y;
for (int i = 0; i < N; i++) {
cin >> x >> y;
beeper.push_back({ x,y });
}
sort(beeper.begin(), beeper.end());
int ans = INT_MAX;
do {
int temp = get_distance();
if (temp < ans)
ans = temp;
} while (next_permutation(beeper.begin(), beeper.end()));
cout << "The shortest path has length ";
cout << ans << "\n";
}
return 0;
}
|
๊ฒฐ๊ตญ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ํ์ํด์ ํ ์ ์๋ ์ฌ์ด ๋ฌธ์ ์๋ค. ํ๋ฌดํ์ง๋ง next_permutation์ด๋ผ๋ ์ข์ ์คํฌ์ ์ป์๋ค.
'๋ฐฑ์ค > Silver' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 19539 : ์ฌ๊ณผ๋๋ฌด (0) | 2022.03.22 |
---|---|
[BOJ] 1697 : ์จ๋ฐ๊ผญ์ง (0) | 2022.01.20 |