μ•Œκ³ λ¦¬μ¦˜/λ°±μ€€

[ BOJ / λ°±μ€€ 17837 ] μƒˆλ‘œμš΄ κ²Œμž„ 2

KIMHYEYUN 2022. 2. 22. 16:49
λ°˜μ‘ν˜•

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

 

17837번: μƒˆλ‘œμš΄ κ²Œμž„ 2

μž¬ν˜„μ΄λŠ” 주변을 μ‚΄νŽ΄λ³΄λ˜ 쀑 체슀판과 말을 μ΄μš©ν•΄μ„œ μƒˆλ‘œμš΄ κ²Œμž„μ„ λ§Œλ“€κΈ°λ‘œ ν–ˆλ‹€. μƒˆλ‘œμš΄ κ²Œμž„μ€ 크기가 N×N인 μ²΄μŠ€νŒμ—μ„œ μ§„ν–‰λ˜κ³ , μ‚¬μš©ν•˜λŠ” 말의 κ°œμˆ˜λŠ” Kκ°œμ΄λ‹€. λ§μ€ μ›νŒλͺ¨μ–‘이고, ν•˜

www.acmicpc.net

문제

μž¬ν˜„μ΄λŠ” 주변을 μ‚΄νŽ΄λ³΄λ˜ 쀑 체슀판과 말을 μ΄μš©ν•΄μ„œ μƒˆλ‘œμš΄ κ²Œμž„μ„ λ§Œλ“€κΈ°λ‘œ ν–ˆλ‹€. μƒˆλ‘œμš΄ κ²Œμž„μ€ 크기가 N×N인 μ²΄μŠ€νŒμ—μ„œ μ§„ν–‰λ˜κ³ , μ‚¬μš©ν•˜λŠ” 말의 κ°œμˆ˜λŠ” Kκ°œμ΄λ‹€. 말은 μ›νŒλͺ¨μ–‘이고, ν•˜λ‚˜μ˜ 말 μœ„μ— λ‹€λ₯Έ 말을 올릴 수 μžˆλ‹€. 체슀판의 각 칸은 흰색, 빨간색, νŒŒλž€μƒ‰ 쀑 ν•˜λ‚˜λ‘œ μƒ‰μΉ λ˜μ–΄μžˆλ‹€.

κ²Œμž„μ€ 체슀판 μœ„μ— 말 K개λ₯Ό 놓고 μ‹œμž‘ν•œλ‹€. 말은 1λ²ˆλΆ€ν„° Kλ²ˆκΉŒμ§€ λ²ˆν˜Έκ°€ 맀겨져 있고, 이동 λ°©ν–₯도 미리 μ •ν•΄μ Έ μžˆλ‹€. 이동 λ°©ν–₯은 μœ„, μ•„λž˜, μ™Όμͺ½, 였λ₯Έμͺ½ 4κ°€μ§€ 쀑 ν•˜λ‚˜μ΄λ‹€.

ν„΄ ν•œ λ²ˆμ€ 1번 말뢀터 K번 λ§κΉŒμ§€ μˆœμ„œλŒ€λ‘œ μ΄λ™μ‹œν‚€λŠ” 것이닀. ν•œ 말이 이동할 λ•Œ μœ„μ— 올렀져 μžˆλŠ” 말도 ν•¨κ»˜ μ΄λ™ν•œλ‹€. 말의 이동 λ°©ν–₯에 μžˆλŠ” 칸에 λ”°λΌμ„œ 말의 이동이 λ‹€λ₯΄λ©° μ•„λž˜μ™€ κ°™λ‹€. 턴이 μ§„ν–‰λ˜λ˜ 쀑에 말이 4개 이상 μŒ“μ΄λŠ” μˆœκ°„ κ²Œμž„μ΄ μ’…λ£Œλœλ‹€.

  • A번 말이 μ΄λ™ν•˜λ €λŠ” 칸이
    • 흰색인 κ²½μš°μ—λŠ” κ·Έ 칸으둜 μ΄λ™ν•œλ‹€. μ΄λ™ν•˜λ €λŠ” 칸에 말이 이미 μžˆλŠ” κ²½μš°μ—λŠ” κ°€μž₯ μœ„μ— A번 말을 μ˜¬λ €λ†“λŠ”λ‹€.
      • A번 말의 μœ„μ— λ‹€λ₯Έ 말이 μžˆλŠ” κ²½μš°μ—λŠ” A번 말과 μœ„μ— μžˆλŠ” λͺ¨λ“  말이 μ΄λ™ν•œλ‹€.
      • 예λ₯Ό λ“€μ–΄, A, B, C둜 μŒ“μ—¬μžˆκ³ , μ΄λ™ν•˜λ €λŠ” 칸에 D, Eκ°€ μžˆλŠ” κ²½μš°μ—λŠ” A번 말이 μ΄λ™ν•œ ν›„μ—λŠ” D, E, A, B, Cκ°€ λœλ‹€.
    • 빨간색인 κ²½μš°μ—λŠ” μ΄λ™ν•œ 후에 A번 말과 κ·Έ μœ„μ— μžˆλŠ” λͺ¨λ“  말의 μŒ“μ—¬μžˆλŠ” μˆœμ„œλ₯Ό λ°˜λŒ€λ‘œ λ°”κΎΌλ‹€.
      • A, B, Cκ°€ μ΄λ™ν•˜κ³ , μ΄λ™ν•˜λ €λŠ” 칸에 말이 μ—†λŠ” κ²½μš°μ—λŠ” C, B, Aκ°€ λœλ‹€.
      • A, D, F, Gκ°€ μ΄λ™ν•˜κ³ , μ΄λ™ν•˜λ €λŠ” 칸에 말이 E, C, B둜 μžˆλŠ” κ²½μš°μ—λŠ” E, C, B, G, F, D, Aκ°€ λœλ‹€.
    • νŒŒλž€μƒ‰μΈ κ²½μš°μ—λŠ” A번 말의 이동 λ°©ν–₯을 λ°˜λŒ€λ‘œ ν•˜κ³  ν•œ μΉΈ μ΄λ™ν•œλ‹€. λ°©ν–₯을 λ°˜λŒ€λ‘œ λ°”κΎΌ 후에 μ΄λ™ν•˜λ €λŠ” 칸이 νŒŒλž€μƒ‰μΈ κ²½μš°μ—λŠ” μ΄λ™ν•˜μ§€ μ•Šκ³  κ°€λ§Œνžˆ μžˆλŠ”λ‹€.
    • μ²΄μŠ€νŒμ„ λ²—μ–΄λ‚˜λŠ” κ²½μš°μ—λŠ” νŒŒλž€μƒ‰κ³Ό 같은 κ²½μš°μ΄λ‹€.

λ‹€μŒμ€ 크기가 4×4인 체슀판 μœ„μ— 말이 4개 μžˆλŠ” κ²½μš°μ΄λ‹€.

첫 번째 턴은 λ‹€μŒκ³Ό 같이 μ§„ν–‰λœλ‹€.

두 번째 턴은 λ‹€μŒκ³Ό 같이 μ§„ν–‰λœλ‹€.

체슀판의 크기와 말의 μœ„μΉ˜, 이동 λ°©ν–₯이 λͺ¨λ‘ μ£Όμ–΄μ‘Œμ„ λ•Œ, κ²Œμž„μ΄ μ’…λ£Œλ˜λŠ” ν„΄μ˜ 번호λ₯Ό κ΅¬ν•΄λ³΄μž.

μž…λ ₯

첫째 쀄에 체슀판의 크기 N, 말의 개수 Kκ°€ μ£Όμ–΄μ§„λ‹€. λ‘˜μ§Έ 쀄뢀터 N개의 쀄에 체슀판의 정보가 μ£Όμ–΄μ§„λ‹€. 체슀판의 μ •λ³΄λŠ” μ •μˆ˜λ‘œ 이루어져 있고, 각 μ •μˆ˜λŠ” 칸의 색을 μ˜λ―Έν•œλ‹€. 0은 흰색, 1은 빨간색, 2λŠ” νŒŒλž€μƒ‰μ΄λ‹€.

λ‹€μŒ K개의 쀄에 말의 정보가 1번 말뢀터 μˆœμ„œλŒ€λ‘œ μ£Όμ–΄μ§„λ‹€. 말의 μ •λ³΄λŠ” μ„Έ 개의 μ •μˆ˜λ‘œ 이루어져 있고, μˆœμ„œλŒ€λ‘œ ν–‰, μ—΄μ˜ 번호, 이동 λ°©ν–₯이닀. ν–‰κ³Ό μ—΄μ˜ λ²ˆν˜ΈλŠ” 1λΆ€ν„° μ‹œμž‘ν•˜κ³ , 이동 λ°©ν–₯은 4보닀 μž‘κ±°λ‚˜ 같은 μžμ—°μˆ˜μ΄κ³  1λΆ€ν„° μˆœμ„œλŒ€λ‘œ →, ←, ↑, ↓의 의미λ₯Ό κ°–λŠ”λ‹€.

같은 칸에 말이 두 개 이상 μžˆλŠ” κ²½μš°λŠ” μž…λ ₯으둜 μ£Όμ–΄μ§€μ§€ μ•ŠλŠ”λ‹€.

좜λ ₯

κ²Œμž„μ΄ μ’…λ£Œλ˜λŠ” ν„΄μ˜ 번호λ₯Ό 좜λ ₯ν•œλ‹€. κ·Έ 값이 1,000보닀 ν¬κ±°λ‚˜ μ ˆλŒ€λ‘œ κ²Œμž„μ΄ μ’…λ£Œλ˜μ§€ μ•ŠλŠ” κ²½μš°μ—λŠ” -1을 좜λ ₯ν•œλ‹€.

μ œν•œ

  • $4 ≤ N ≤ 12$
  • $4 ≤ K ≤ 10$

문제 풀이

1) 체슀판의 색상 정보와 말의 정보λ₯Ό 뢄리

int[][] color : 색상 정보
piece[] pieces : 각 말의 λ²ˆν˜Έλ³„ 정보
List<Integer>[][] map : 체슀판 μœ„μ˜ 말의 번호

2) κ²Œμž„ μ‹œμž‘ game()

1000초 초과되면 μ’…λ£Œ πŸ‘‰ -1 좜λ ₯

말 μˆœμ„œλŒ€λ‘œ 이동 μ‹œμž‘ πŸ‘‰ move(i)

3) 말 이동 move(int i)

i번 말의 이동 λ°©ν–₯λŒ€λ‘œ 이동

λ‹€μŒ 칸이 λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜κ±°λ‚˜, 색이 νŒŒλž€μƒ‰μ΄λΌλ©΄
πŸ‘‰ λ°©ν–₯을 λ°˜λŒ€λ‘œν•΄μ„œ λ‹€μ‹œ 이동
βœ… λ°˜λŒ€ λ°©ν–₯도 νŒŒλž€μƒ‰μ΄κ±°λ‚˜, λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ©΄ 이동 ❌

κ·Έλ ‡μ§€ μ•Šλ‹€λ©΄ β€Ό

4) μƒ‰μƒλ³„λ‘œ 이동 moveByColor(int num, piece now, int nx, int ny)

  1. ν•˜μ–€μƒ‰ → map[nx][ny]에 num번 말뢀터 κ·Έ μœ„μ˜ 말듀을 μˆœμ„œλŒ€λ‘œ μŒ“κΈ°
  2. 빨간색 → map[nx][ny]에 맨 μœ„μ˜ 말뢀터 num번 λ§κΉŒμ§€ μ—­μˆœμœΌλ‘œ μŒ“κΈ°

heightOfNow : now 말(num번 말)의 높이

4-1) ν•˜μ–€μƒ‰ μΉΈ
map[now.x][now.y]μ—μ„œ heightOfNow λΆ€ν„° λ§ˆμ§€λ§‰ 말듀을 map[nx][ny] μœ„λ‘œ 이동

4-2) 빨간색 μΉΈ
map[now.x][now.y]μ—μ„œ λ§ˆμ§€λ§‰ 말뢀터 heightOfNow λ§κΉŒμ§€ map[nx][ny] μœ„λ‘œ 이동

5) μ’…λ£Œ μ—¬λΆ€

map[nx][ny]의 크기가 4이상이면, μ’…λ£Œ~!!!

μ½”λ“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main_BOJ_17838_μƒˆλ‘œμš΄_κ²Œμž„_2 {
    static class piece {
        int x, y, dir;

        public piece(int x, int y, int dir) {
            this.x = x;
            this.y = y;
            this.dir = dir;
        }
    }
    static int N, K;
    static int[][] color;
    static List<Integer>[][] map;
    static piece[] pieces;
    static int[] dx = {0, 0, -1, 1};
    static int[] dy = {1, -1, 0, 0};
    static final int WHITE = 0, RED = 1, BLUE = 2;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());

        N = Integer.parseInt(stringTokenizer.nextToken());
        K = Integer.parseInt(stringTokenizer.nextToken());

        map = new List[N][N];
        pieces = new piece[K];
        color = new int[N][N];

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

        for (int i = 0; i < K; i++) {
            stringTokenizer = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(stringTokenizer.nextToken()) - 1;
            int y = Integer.parseInt(stringTokenizer.nextToken()) - 1;
            int d = Integer.parseInt(stringTokenizer.nextToken()) - 1;

            pieces[i] = new piece(x, y, d);
            map[x][y].add(i);
        }


        game();

    }

    private static void game() {
        for (int time = 1; time <= 1000; time++) {
            for (int i = 0; i < K; i++) {
                if (move(i)) {
                    System.out.println(time);
                    return;
                }

            }
        }
        System.out.println(-1);
    }

    private static boolean move(int i) {
        piece now = pieces[i];

        int nx = now.x + dx[now.dir];
        int ny = now.y + dy[now.dir];

        if (nx < 0 || ny < 0 || N <= nx || N <= ny || color[nx][ny] == BLUE) {
            changeDir(now);
            nx = now.x + dx[now.dir];
            ny = now.y + dy[now.dir];
        }

        if ((0 <= nx && nx < N) && (0 <= ny && ny < N) && color[nx][ny] != BLUE) {
            moveByColor(i, now, nx, ny);

            if(map[nx][ny].size() > 3) return true;
        }
        return false;
    }

    private static void moveByColor(int num, piece now, int nx, int ny) {
        List<Integer> from = map[now.x][now.y];
        List<Integer> to = map[nx][ny];

        int heightOfNow = from.indexOf(num);

        switch (color[nx][ny]) {
            case WHITE:
                for (int i = heightOfNow; i < from.size(); i++) {
                    int piece = from.get(i);
                    updateList(piece, nx, ny);
                    to.add(piece);
                }

                removePiece(from, from.size() - 1, heightOfNow);
                break;

            case RED:
                for (int i = from.size() - 1; i >= heightOfNow; i--) {
                    int piece = from.get(i);
                    updateList(piece, nx, ny);
                    to.add(piece);
                }

                removePiece(from, from.size() - 1, heightOfNow);
                break;
        }
    }

    private static void removePiece(List<Integer> list, int top, int bottom) {
        for (int i = top; i >= bottom; i--) {
            list.remove(i);
        }
    }

    private static void updateList(int piece, int nx, int ny) {
        pieces[piece].x = nx;
        pieces[piece].y = ny;
    }

    private static void changeDir(piece now) {
        if(now.dir == 0) now.dir = 1;
        else if(now.dir == 1) now.dir = 0;
        else if(now.dir == 2) now.dir = 3;
        else now.dir = 2;
    }
}
728x90
λ°˜μ‘ν˜•