๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Silver

[BOJ] 6716 : Collecting Beepers

by Jaeguk 2022. 1. 19.
๋ฌธ์ œ ๋งํฌ

6716๋ฒˆ: Collecting Beepers (acmicpc.net)

 

6716๋ฒˆ: Collecting Beepers

Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in her world. To do so

www.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, 0sizeof(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์ด๋ผ๋Š” ์ข‹์€ ์Šคํ‚ฌ์„ ์–ป์—ˆ๋‹ค.

728x90

'๋ฐฑ์ค€ > Silver' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] 19539 : ์‚ฌ๊ณผ๋‚˜๋ฌด  (0) 2022.03.22
[BOJ] 1697 : ์ˆจ๋ฐ”๊ผญ์งˆ  (0) 2022.01.20