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

[BOJ] 14499 : ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ

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

https://www.acmicpc.net/problem/14499

 

14499๋ฒˆ: ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ

์ฒซ์งธ ์ค„์— ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ N, ๊ฐ€๋กœ ํฌ๊ธฐ M (1 ≤ N, M ≤ 20), ์ฃผ์‚ฌ์œ„๋ฅผ ๋†“์€ ๊ณณ์˜ ์ขŒํ‘œ x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), ๊ทธ๋ฆฌ๊ณ  ๋ช…๋ น์˜ ๊ฐœ์ˆ˜ K (1 ≤ K ≤ 1,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ง€

www.acmicpc.net

ํŠน๋ณ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์—†์ด ํ’€ ์ˆ˜ ์žˆ๋Š” ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ.

ํ’€์ด

์ •๋ง ํŠน๋ณ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์—†์ด ๋ช…๋ น์ด 1(๋™์ชฝ), 2(์„œ์ชฝ), 3(๋ถ์ชฝ), 4(๋‚จ์ชฝ) ์ค‘์— ๋ฌด์—‡์ธ์ง€์— ๋”ฐ๋ผ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด๋œ๋‹ค. ๋งŒ์•ฝ ์ด๋™ํ–ˆ์„ ๋•Œ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด ์Šคํ‚ตํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

4๋ฐฉํ–ฅ์œผ๋กœ ๊ตด๋ ธ์„ ๋•Œ ๊ฐ ๋ฉด์ด ์–ด๋””๋ฉด์œผ๋กœ ์ด๋™ํ•˜๋Š”์ง€๋งŒ ์ž˜ ์ƒ๊ฐํ•ด๋ณด๋ฉด ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ฒ˜์Œ์— 1๋ฒˆ์ด ์œ„๋ฅผ ๋ณด๊ณ  3๋ฒˆ์ด ๋™์ชฝ์„ ๋ณด๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ๋™์ชฝ์œผ๋กœ ๊ตด๋ฆฌ๋ฉด 1๋ฒˆ์ด ๋™์ชฝ์„ ๋ณด๊ฒŒ๋˜๊ณ  3๋ฒˆ์€ ๋ฐ”๋‹ฅ์œผ๋กœ ํ–ฅํ•˜๊ฒŒ ๋œ๋‹ค.

 

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
#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 Map[MAX_N][MAX_N];
int dice[7];
int N, M , K;
int Top, Bottom, East, West, North, South;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,-1,1 };
 
void Rolling_Dice(int cmd, pair<int,int> next) {
    int New_Top = 0, New_Bottom = 0, New_West = 0, New_North = 0, New_South = 0, New_East = 0;
    if (cmd == 1) {
        New_Bottom = East;
        New_East = Top;
        New_Top = West;
        New_West = Bottom;
        New_North = North;
        New_South = South;
    }
    else if (cmd == 2) {
        New_West = Top;
        New_Top = East;
        New_Bottom = West;
        New_East = Bottom;
        New_North = North;
        New_South = South;
    }
    else if (cmd == 3) {
        New_North = Top;
        New_Top = South;
        New_Bottom = North;
        New_South = Bottom;
        New_East = East;
        New_West = West;
    }
    else if (cmd == 4) {
        New_Top = North;
        New_South = Top;
        New_Bottom = South;
        New_North = Bottom;
        New_East = East;
        New_West = West;
    }
    Top = New_Top;
    South = New_South;
    North = New_North;
    Bottom = New_Bottom;
    East = New_East; 
    West = New_West;
 
    if (Map[next.first][next.second] != 0) {
        dice[Bottom] = Map[next.first][next.second];
        Map[next.first][next.second] = 0;
    }
    else if (Map[next.first][next.second] == 0) {
        Map[next.first][next.second] = dice[Bottom];
    }
}
 
bool is_ok(int y, int x) {
    if (y < 0 || y >= N || x < 0 || x >= M)
        return false;
    return true;
}
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    pair<intint> now;
    int X, Y;
    cin >> N >> M >> Y  >> X >> K;
    now.first = Y;
    now.second = X;
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> Map[i][j];
        }
    }
 
    Top = 1;
    Bottom = 6;
    East = 3;
    West = 4;
    North = 2;
    South = 5;
 
    for (int i = 0; i < K; i++) {
        int cmd;
        cin >> cmd;
        pair<intint> next;
        next = { now.first + dy[cmd-1], now.second + dx[cmd-1] };
        if (!is_ok(next.first, next.second))
            continue;
        Rolling_Dice(cmd, next);
        now = next;
        cout << dice[Top] << "\n";
    }
    return 0;
}
 
cs

์ฒ˜์Œ์— ์•„๋ฌด๋ฆฌ๋ด๋„ ๋งž๋Š” ์ฝ”๋“œ์ธ๋ฐ 50%์—์„œ ํ‹€๋ฆฌ๋Š” ๊ฒƒ๋„ ์•„๋‹ˆ๊ณ  ์‹œ์ž‘ํ•˜์ž๋งˆ์ž ํ‹€๋ ค์„œ ํ•œ์ฐธ์„ ์ฝ”๋“œ๋ฅผ ๋“ค์—ฌ๋‹ค๋ดค๋‹ค. ์˜ˆ์ œ๋„ 4๊ฐœ ๋‹ค ์ž˜๋‚˜์˜ค๋Š”๋ฐ ๊ณ„์† ํ‹€๋ ค์„œ ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์—ˆ๋‹ค.

๋ณดํ†ต y๊ฐ€ ์„ธ๋กœ์ถ• ์ขŒํ‘œ๋ฅผ ์˜๋ฏธํ•˜๊ณ  x๊ฐ€ ๊ฐ€๋กœ์ถ• ์ขŒํ‘œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๊ฒŒ ์ผ๋ฐ˜์ ์ด๋‹ค. (y,x) ์ด๋ ‡๊ฒŒ ๋ง์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ฒ˜์Œ์— x ์ž…๋ ฅ ํ›„ y๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๊ณ  ํ•ด์„œ ๊ฐ€๋กœ์ถ• ์ขŒํ‘œ ๋จผ์ € ์ž…๋ ฅ๋ฐ›๊ณ  ์„ธ๋กœ์ถ• ์ขŒํ‘œ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๊ตฌ๋‚˜ ์ƒ๊ฐํ–ˆ๋‹ค. 

์ฃผ์‚ฌ์œ„๋ฅผ ๋†“์€ ๊ณณ์˜ ์ขŒํ‘œ x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1)

N์ด ์„ธ๋กœ ์ตœ๋Œ€ ๋ฒ”์œ„, M์ด ๊ฐ€๋กœ์˜ ๋ฒ”์œ„์ธ๋ฐ, ๋ฒ”์œ„๋ฅผ ๋ณด๋ฉด X๊ฐ€ 0~N-1์‚ฌ์ด ์ฆ‰, ์„ธ๋กœ ๋ฒ”์œ„. Y๊ฐ€ 0์—์„œ M-1 ์‚ฌ์ด ์ฆ‰, ๊ฐ€๋กœ ๋ฒ”์œ„์— ์žˆ์—ˆ๋‹ค. ์ถœ์ œ์ž๋Š” ์ขŒํ‘œ๋ฅผ (x,y)๋กœ ๋‘๊ณ  ๋ฌธ์ œ๋ฅผ ๋‚ธ ๊ฒƒ์ด๋‹ค.

์ด๊ฒƒ๋•Œ๋ฌธ์— ํ•œ์ฐธ์„ ์‚ฝ์งˆํ–ˆ๋‹ค.

์˜ˆ์ œ์—์„  x,y ์ขŒํ‘œ๊ฐ€ ๋™์ผํ•œ (1,1), (0,0) ๋“ฑ์„ ๋‚ด์ค˜์„œ ์˜ˆ์ œ๋Š” ๋‹ค ๋งž์•˜๋˜ ๊ฒƒ์ด๋‹ค.

์•„๋งˆ ์ด ๋ฌธ์ œ๋ฅผ ํ—ค๋งค๊ณ  ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์ด ์žˆ๋‹ค๋ฉด ๋Œ€๋ถ€๋ถ„ ๊ฐ™์€ ์ด์œ ์ด์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•œ๋‹ค.

728x90