๊ธฐ๋ก๋ฐฉ

BOJ_14499 : ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_14499 : ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ

Soom_1n 2024. 4. 26. 18:40

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

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



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • N x M ์ง€๋„์—์„œ ์ •์œก๋ฉด์ฒด ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค๊ฐ€๋ฉฐ, ์ฃผ์‚ฌ์œ„ ์œ—๋ฉด์˜ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์ฃผ์‚ฌ์œ„๊ฐ€ ์ด๋™ํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆซ์ž ๋ณ€ํ™”๊ฐ€ ์žˆ๋‹ค.
    • ์ด๋™ํ•œ ์ง€๋„์˜ ์นธ์˜ ์ˆซ์ž๊ฐ€ 0์ด๋ฉด, ๋งž๋‹ฟ์•„ ์žˆ๋Š” ์ฃผ์‚ฌ์œ„ ์•„๋žซ๋ฉด์˜ ์ˆซ์ž๊ฐ€ ์ง€๋„๋กœ ๋ณต์‚ฌ๋œ๋‹ค.
    • ์ด๋™ํ•œ ์ง€๋„์˜ ์นธ์˜ ์ˆซ์ž๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด, ์ง€๋„์˜ ์ˆซ์ž๊ฐ€ ์ฃผ์‚ฌ์œ„ ์•„๋žซ๋ฉด์œผ๋กœ ๋ณต์‚ฌ๋œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ์ฃผ์‚ฌ์œ„์˜ ํšŒ์ „์— ๋”ฐ๋ฅธ ์ˆซ์ž ์ด๋™์„ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ์ง€๋„์ƒ์˜ ์ด๋™์„ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ์ง€๋„๋ฅผ ๋ฒ—์–ด๋‚œ ์ด๋™์€ ๋ฌด์‹œํ•˜๊ณ , ์ด๋™ ํ–ˆ์„ ๊ฒฝ์šฐ๋งŒ ์ฃผ์‚ฌ์œ„ ์œ„์ชฝ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    private static final int[][] md = {
            {0, 0, 0, 0}, // ์ธ๋ฑ์Šค์šฉ
            {0, 4, 5, 2}, // right
            {0, 2, 5, 4}, // left
            {0, 1, 5, 3}, // up
            {0, 3, 5, 1}, // down
    };

    private static final int[] dx = {0, 0, 0, -1, 1};
    private static final int[] dy = {0, 1, -1, 0, 0};

    private static int[] dice; // ํ˜„์žฌ ์ฃผ์‚ฌ์œ„ ์ˆซ์ž : ์ƒ ๋‚จ ์„œ ๋ถ ๋™ ํ•˜

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        StringBuilder sb = new StringBuilder();

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int row = Integer.parseInt(st.nextToken());
        int col = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[][] map = new int[N][M];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        // Game
        dice = new int[6];
        st = new StringTokenizer(br.readLine());
        while (st.hasMoreTokens()) {
            int next = Integer.parseInt(st.nextToken());
            int x = row + dx[next];
            int y = col + dy[next];
            if (0 <= x && x < N && 0 <= y && y < M) {
                move(next);
                row = x;
                col = y;
                if (map[row][col] == 0) {
                    map[row][col] = dice[5];
                } else {
                    dice[5] = map[row][col];
                    map[row][col] = 0;
                }
                sb.append(dice[0]).append('\n');
            }
        }

        // Output
        bw.write(sb.toString());
        bw.flush();
    }

    static private void move(int d) {
        int temp = dice[md[d][0]];
        dice[md[d][0]] = dice[md[d][1]];
        dice[md[d][1]] = dice[md[d][2]];
        dice[md[d][2]] = dice[md[d][3]];
        dice[md[d][3]] = temp;
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ฃผ์‚ฌ์œ„ ์ด๋™์„ ์œ„ํ•œ int 2์ฐจ์› ๋ฐฐ์—ด md๋ฅผ ๋งŒ๋“ค์–ด move() ๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ์ง€๋„ ์ด๋™์„ ์œ„ํ•ด int ๋ฐฐ์—ด dx, dy ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ์ด๋™ ์‹œ์—๋งŒ ์ฃผ์‚ฌ์œ„ ์ƒ๋‹จ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์— ์œ ์˜ํ•ด์„œ ๋ฌธ์ œ ๋‚ด์šฉ์„ ๊ตฌํ˜„ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ด๋™ ์‹œ์—๋งŒ ์ฃผ์‚ฌ์œ„ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์กฐ๊ฑด์„ ๋น ๋œจ๋ ค 1ํšŒ ํ‹€๋ ธ๋‹ค.
  • ๋ฌธ์ œ์— ์ •์œก๋ฉด์ฒด ์ฃผ์‚ฌ์œ„ ์ „๊ฐœ๋„๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š”๋ฐ, 1~6 ์ˆซ์ž๊ฐ€ ์“ฐ์—ฌ์žˆ์–ด์„œ ์ดˆ๊ธฐํ™” ํ•ด์•ผ ํ•˜๋Š” ์ค„ ์•Œ์•˜๋‹ค.
    • ์ „๊ฐœ๋„๋Š” ๊ทธ๋ƒฅ ํžŒํŠธ๋ฅผ ์œ„ํ•ด ์ œ๊ณต ๋œ ๊ฒƒ์ด๊ณ  0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ–ˆ๋Š”๋ฐ, ๋ฌธ์ œ์—์„œ๋Š” ๋ชจ์ˆœ๋œ ๋ฌธ์žฅ ์•„๋‹Œ๊ฐ€ ํ•ด์„œ ์˜ค์—ญ/์˜คํƒ€ ์ˆ˜์ •์„ ์‹ ์ฒญํ•˜๋ ค ํ–ˆ์ง€๋งŒ ๊ธฐ์กด ๊ธ€์ด ์žˆ์—ˆ๋‹ค. ( https://www.acmicpc.net/board/view/33943 )
    • ์ถฉ๋ถ„ํžˆ ๋…ผ์˜ ๋๋˜ ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™์•„ ์‹ ์ฒญ์€ ์•ˆํ–ˆ๋‹ค.
    • ์—ฌ๋Ÿฌ ์˜คํƒ€/์˜ค์—ญ/์š”์ฒญ ๊ธ€์„ ๋ณด๋‹ˆ ๋ฌธ์ œ๋ฅผ ๋งŒ๋“ค ๋•Œ ์–ด๋–ค ์ ์„ ๊ณ ๋ คํ•˜๋Š”์ง€, ๋ฐฑ์ค€ ์‚ฌ์ดํŠธ์˜ ์šด์˜ ๋ฐฉ๋ฒ• ๋“ฑ์— ๋Œ€ํ•ด ์ข€ ๋” ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™๋‹ค.
728x90

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

BOJ_1922 : ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ  (0) 2024.04.30
BOJ_2573 : ๋น™์‚ฐ  (0) 2024.04.27
BOJ_1976 : ์—ฌํ–‰ ๊ฐ€์ž  (0) 2024.04.22
BOJ_2580 : ์Šค๋„์ฟ   (0) 2024.04.19
BOJ_1707 : ์ด๋ถ„ ๊ทธ๋ž˜ํ”„  (0) 2024.04.13