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

[BOJ] 17780 : ์ƒˆ๋กœ์šด ๊ฒŒ์ž„

by Jaeguk 2022. 4. 15.
๋ฌธ์ œ ๋งํฌ

17780๋ฒˆ: ์ƒˆ๋กœ์šด ๊ฒŒ์ž„ (acmicpc.net)

 

17780๋ฒˆ: ์ƒˆ๋กœ์šด ๊ฒŒ์ž„

์žฌํ˜„์ด๋Š” ์ฃผ๋ณ€์„ ์‚ดํŽด๋ณด๋˜ ์ค‘ ์ฒด์ŠคํŒ๊ณผ ๋ง์„ ์ด์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ๊ฒŒ์ž„์„ ๋งŒ๋“ค๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ƒˆ๋กœ์šด ๊ฒŒ์ž„์€ ํฌ๊ธฐ๊ฐ€ N×N์ธ ์ฒด์ŠคํŒ์—์„œ ์ง„ํ–‰๋˜๊ณ , ์‚ฌ์šฉํ•˜๋Š” ๋ง์˜ ๊ฐœ์ˆ˜๋Š” K๊ฐœ์ด๋‹ค. ๋ง์€ ์›ํŒ๋ชจ์–‘์ด๊ณ , ํ•˜

www.acmicpc.net

์žฌํ˜„์ด๊ฐ€ ์ฒด์ŠคํŒ์œผ๋กœ ๋งŒ๋“  ์ƒˆ๋กœ์šด ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜๊ธฐ๊นŒ์ง€ ๋ช‡ ํ„ด์ด ๊ฑธ๋ฆฌ๋Š”์ง€ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ.

์š”์ฆ˜ ๋ฐฑ์ค€ ๋ฌธ์ œ์ง‘ ์ค‘์—์„œ danimartinwife ๋‹˜์ด ์˜ฌ๋ ค์ฃผ์‹  โ˜…์ง์ ‘ ์ฝ”ํ…Œ ๊ด‘ํƒˆํ•˜๋ฉด์„œ ๋ชจ์€ ๋ฌธ์ œ๋“คโ˜† ์ด๋ผ๋Š” ๋ฌธ์ œ์ง‘์„ ํ’€์–ด๋ณด๊ณ  ์žˆ๋Š”๋ฐ ์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ๋“ค์ด ๋งŽ์€ ๊ฒƒ๊ฐ™๋‹ค.

ํ’€์ด

๊ทœ์น™์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด

์ด K๊ฐœ์˜ ๋ง์ด ์žˆ๋Š”๋ฐ ํ•œ ํ„ด์— 1๋ฒˆ๋ง๋ถ€ํ„ฐ K๋ฒˆ๋ง๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์›€์ง์ธ๋‹ค. ๋‹จ, ๋ง์€ ์—ฌ๋Ÿฌ๊ฐœ ๊ฒน์น  ์ˆ˜ ์žˆ๊ณ  ๋งŒ์•ฝ ๋ง์ด ๊ฒน์ณ์ง„ ๊ฒฝ์šฐ ๋งจ ์•„๋ž˜์— ์žˆ๋Š” ๋ง๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ์œ„์— ์Œ“์—ฌ์žˆ๋Š” ๋ง๋„ ๊ฐ™์ด ์ด๋™ํ•œ๋‹ค. ๋ง์ด 4๊ฐœ ์ด์ƒ ์Œ“์ธ ์นธ์ด ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด ์ฆ‰์‹œ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ์ด๋™ํ•  ์นธ์ด

1. ํฐ์ƒ‰์ด๋ฉด ๊ทธ๋ƒฅ ์ด๋™ํ•œ๋‹ค. ์ด๋™ํ•˜๋ ค๋Š” ์นธ์— ์ด๋ฏธ ๋ง์ด ์žˆ์œผ๋ฉด ์ด ์œ„์— ๊ฒน์ณ์„œ ์Œ“๋Š”๋‹ค.

2. ๋นจ๊ฐ„์ƒ‰์ด๋ฉด ์ด๋™ํ•œ ํ›„์— ์Œ“์—ฌ์žˆ๋Š” ๋ง๋“ค์„ ๋’ค์ง‘๋Š”๋‹ค. ๋งŒ์•ฝ 1์ด ์ œ์ผ ๋ฐ‘์— ์žˆ๊ณ  1,2,3 ์ˆœ์œผ๋กœ ์Œ“์—ฌ์žˆ์—ˆ๋‹ค๋ฉด 3์ด ์ œ์ผ ์•„๋ž˜๋กœ ๊ฐ€๊ฒŒ๋˜๊ณ  3,2,1 ์ˆœ์„œ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ ์Œ“์—ฌ์ ธ์žˆ๋Š” ์นธ์— ์Œ“์—ฌ์ง„ ๋ง์„ ๊ฒน์ณ ์Œ“์„๋•Œ์—๋Š” ์ƒˆ๋กœ ์Œ“์„ ๋ง๋“ค๋งŒ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พผ๋‹ค.

3. ํŒŒ๋ž€์ƒ‰ ์นธ์ด๊ฑฐ๋‚˜ ํŒ์„ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ ๋ง์˜ ๋ฐฉํ–ฅ์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊พธ๊ณ  ํ•œ ์นธ ์ด๋™ํ•œ๋‹ค. ๊ทธ ์ด๋™ํ•  ์นธ์ด ๋งŒ์•ฝ ๋˜ ํŒŒ๋ž€์ƒ‰ ์นธ์ด๊ฑฐ๋‚˜ ํŒ์„ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด ์ œ์ž๋ฆฌ์— ๊ฐ€๋งŒํžˆ์žˆ๋Š”๋‹ค.

์ค‘์š”ํ•œ ๊ฒƒ์€ ๋ง์ด ์—ฌ๋Ÿฌ๊ฐœ ์Œ“์—ฌ์žˆ์„ ๋•Œ ๋งจ ์•„๋ž˜์— ์žˆ๋Š” ๋ง๋งŒ ์›€์ง์ธ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

1~K๋ฒˆ๊นŒ์ง€ ๋ง์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋งจ ์•„๋ž˜์— ์žˆ๋Š” ๋ง๋งŒ ์ด๋™์‹œํ‚ค๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

for (int i = 1; i <= K; i++) {
			int now_y = Position[i].first;
			int now_x = Position[i].second;
			int horse_num = horse[now_y][now_x][0];
			if (horse_num != i)
				continue;

์—ฌ๊ธฐ์„œ horse ๋Š” ๊ฐ ์นธ์— ์žˆ๋Š” ๋ง์˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•ด๋‘” ๋ฒกํ„ฐ์ด๋‹ค. ์นธ์— ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ๋ฒกํ„ฐ์— ์Œ“์ด๊ธฐ ๋•Œ๋ฌธ์— 0๋ฒˆ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋ง์ด ๊ฐ€์žฅ์•„๋ž˜์— ์žˆ๋Š” ๋ง์„ ๋œปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ horse_num์—๋Š” ๊ฐ€์žฅ ์•„๋ž˜์— ์žˆ๋Š” ๋ง์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ž…๋ ฅ๋˜๊ณ , ์ด ๋ฒˆํ˜ธ๊ฐ€ ํ˜„์žฌ ์›€์ง์ผ ๋ง์˜ ๋ฒˆํ˜ธ์™€ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด continue๋กœ ์Šคํ‚ตํ•œ๋‹ค.

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<intpair<intint>>vector<pair<intpair<intint>>>, greater<pair<intpair<intint>>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 20 + 5;
int N, K;
vector<int> horse[MAX_N][MAX_N];
int Direction[MAX_N];
int Map[MAX_N][MAX_N];
pair<intint> Position[MAX_N];
int dx[] = { 0,1,-1,0,0 };
int dy[] = { 0,0,0,-1,1 };
 
bool is_ok(pair<intint> Position) {
    if (Position.first < 1 || Position.first > N || Position.second < 1 || Position.second > N)
        return false;
    return true;
}
 
void Change_Direction(int horse_num) {
    if (Direction[horse_num] == 1) Direction[horse_num] = 2;
    else if (Direction[horse_num] == 2) Direction[horse_num] = 1;
    else if (Direction[horse_num] == 3) Direction[horse_num] = 4;
    else if (Direction[horse_num] == 4) Direction[horse_num] = 3;
}
 
bool Move(pair<int,int> from, pair<int,int> to, int color) {
    if (color == 0) { //ํฐ์ƒ‰
        for (int i = 0; i < horse[from.first][from.second].size(); i++) {
            horse[to.first][to.second].push_back(horse[from.first][from.second][i]);
            Position[horse[from.first][from.second][i]] = { to.first, to.second };
        }
    }
    else if (color == 1) {
        for (int i = (int)horse[from.first][from.second].size() - 1; i >= 0; i--) {
            horse[to.first][to.second].push_back(horse[from.first][from.second][i]);
            Position[horse[from.first][from.second][i]] = { to.first, to.second };
        }
    }
    horse[from.first][from.second].clear();
    if (horse[to.first][to.second].size() >= 4)
        return false;
    return true;
}
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> K;
 
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++)
            cin >> Map[i][j];
    }
 
    for (int i = 1; i <= K; i++) {
        int X, Y, D;
        cin >> Y >> X >> D;
        horse[Y][X].push_back(i);
        Position[i] = { Y,X };
        Direction[i] = D;
    }
    int turn = 0;
    while (true) {
        turn++;
        if (turn > 1000)
            break;
        bool chk = true;
        for (int i = 1; i <= K; i++) {
            int now_y = Position[i].first;
            int now_x = Position[i].second;
            int horse_num = horse[now_y][now_x][0];
            if (horse_num != i)
                continue;
            pair<intint> next = { now_y + dy[Direction[horse_num]], now_x + dx[Direction[horse_num]] };
            if (!is_ok(next) || Map[next.first][next.second] == 2) {
                Change_Direction(horse_num);
                next = { now_y + dy[Direction[horse_num]],now_x + dx[Direction[horse_num]] };
                if (!is_ok(next) || Map[next.first][next.second] == 2)
                    continue;
                else if (!Move({ now_y,now_x }, next, Map[next.first][next.second])) {
                    chk = false;
                    break;
                }
            }
            else {
                if (!Move({ now_y,now_x }, next, Map[next.first][next.second])) {
                    chk = false;
                    break;
                }
            }
        }
        if (!chk)
            break;
    }
    if (turn > 1000)
        cout << "-1\n";
    else cout << turn << "\n";
    return 0;
}
cs

 

728x90