문제 μ„€λͺ…


πŸ’‘ Info

λ‚΄μš©

μ„œμ–‘ μž₯기인 μ²΄μŠ€μ—λŠ” λŒ€κ°μ„  λ°©ν–₯으둜 움직일 수 μžˆλŠ” λΉ„μˆ(bishop)이 μžˆλ‹€. < κ·Έλ¦Ό 1 >κ³Ό 같은 μ •μ‚¬κ°ν˜• 체슀판 μœ„μ— B라고 ν‘œμ‹œλœ 곳에 λΉ„μˆμ΄ μžˆμ„ λ•Œ λΉ„μˆμ€ λŒ€κ°μ„  λ°©ν–₯으둜 움직여 O둜 ν‘œμ‹œλœ 칸에 μžˆλŠ” λ‹€λ₯Έ 말을 μž‘μ„ 수 μžˆλ‹€.

그런데 체슀판 μœ„μ—λŠ” λΉ„μˆμ΄ 놓일 수 μ—†λŠ” 곳이 μžˆλ‹€. < κ·Έλ¦Ό 2 >μ—μ„œ μ²΄μŠ€νŒμ— μƒ‰μΉ λœ 뢀뢄은 λΉ„μˆμ΄ 놓일 수 μ—†λ‹€κ³  ν•˜μž. 이와 같은 μ²΄μŠ€νŒμ— μ„œλ‘œκ°€ μ„œλ‘œλ₯Ό μž‘μ„ 수 없도둝 ν•˜λ©΄μ„œ λΉ„μˆμ„ λ†“λŠ”λ‹€λ©΄ < κ·Έλ¦Ό 3 >κ³Ό 같이 μ΅œλŒ€ 7개의 λΉ„μˆμ„ 놓을 수 μžˆλ‹€. μƒ‰μΉ λœ λΆ€λΆ„μ—λŠ” λΉ„μˆμ΄ 놓일 수 μ—†μ§€λ§Œ μ§€λ‚˜κ°ˆ μˆ˜λŠ” μžˆλ‹€.

μ •μ‚¬κ°ν˜• 체슀판의 ν•œ 변에 놓인 칸의 개수λ₯Ό 체슀판의 크기라고 ν•œλ‹€. 체슀판의 크기와 체슀판 각 칸에 λΉ„μˆμ„ 놓을 수 μžˆλŠ”μ§€ μ—†λŠ”μ§€μ— λŒ€ν•œ 정보가 μ£Όμ–΄μ§ˆ λ•Œ, μ„œλ‘œκ°€ μ„œλ‘œλ₯Ό μž‘μ„ 수 μ—†λŠ” μœ„μΉ˜μ— 놓을 수 μžˆλŠ” λΉ„μˆμ˜ μ΅œλŒ€ 개수λ₯Ό κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

πŸ“₯μž…λ ₯ 쑰건

  • 첫째 쀄에 체슀판의 크기가 주어진닀. 체슀판의 ν¬κΈ°λŠ” 10μ΄ν•˜μ˜ μžμ—°μˆ˜μ΄λ‹€.
  • λ‘˜μ§Έ 쀄뢀터 μ•„λž˜μ˜ μ˜ˆμ™€ 같이 체슀판의 각 칸에 λΉ„μˆμ„ 놓을 수 μžˆλŠ”μ§€ μ—†λŠ”μ§€μ— λŒ€ν•œ 정보가 체슀판 ν•œ 쀄 λ‹¨μœ„λ‘œ ν•œ 쀄씩 주어진닀.
  • λΉ„μˆμ„ 놓을 수 μžˆλŠ” κ³³μ—λŠ” 1, λΉ„μˆμ„ 놓을 수 μ—†λŠ” κ³³μ—λŠ” 0이 λΉˆμΉΈμ„ 사이에 두고 주어진닀.
5
1 1 0 1 1
0 1 0 0 0
1 0 1 0 1
1 0 0 0 0
1 0 1 1 1

πŸ“€μΆœλ ₯ 쑰건

  • 첫째 쀄에 주어진 체슀판 μœ„μ— 놓을 수 μžˆλŠ” λΉ„μˆμ˜ μ΅œλŒ€ 개수λ₯Ό 좜λ ₯ν•œλ‹€.
7


문제 이해


  • λΉ„μˆμ΄ κ°€λŠ₯ν•œ μ΅œλŒ€μ˜ 수λ₯Ό μ°ΎλŠ” 문제


μ•Œκ³ λ¦¬μ¦˜ 및 μ΅œμ’… 풀이


μ‹€μ œ 풀이 μ‹œκ°„ : 1μ‹œκ°„ 2λΆ„(첫 풀이 μ‹œκ°„ 포함)

  • μž…λ ₯

    • N, λΉ„μˆ κ°€λŠ₯ μ—¬λΆ€ μž…λ ₯
  • 계산

    • initializeColorPattern()
      • μΉΈ 색상 κ²°μ •
    • κ²€μ •, 흰색에 λ”°λΌμ„œ backtrack()둜 κ°€λŠ₯ν•œ μ΅œλŒ€ λΉ„μˆ 개수 κ΅¬ν•˜κΈ°
      • λΉ„μˆμ„ 놓아도 λ˜λŠ”μ§€ 확인 -> λœλ‹€λ©΄ 놓고 λ‚œ ν›„ μž¬κ·€ 돌리기
      • 색상별 μ΅œλŒ€ λΉ„μˆ 개수 μ €μž₯(maxBlackBishops, maxWhiteBishops)
  • 좜λ ₯

    • κ²€μ •κ³Ό 흰색에 놓을 수 μžˆλŠ” μ΅œλŒ€ λΉ„μˆμ˜ 개수λ₯Ό ν•©ν•΄μ„œ 좜λ ₯
import java.io.*;
import java.util.*;

public class Main {
    static int N;
    static int maxBishops;
    static int maxBlackBishops;
    static int maxWhiteBishops;

    static int[][] board;
    static int[][] colorPattern;
    static boolean[][] occupied;
    static int[][] directions = {{-1,-1},{-1,1},{1,-1},{1,1}};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        board = new int[N][N];
        occupied = new boolean[N][N];
        
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                board[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        
        colorPattern = new int[N][N];
        initializeColorPattern();

        backtrack(0, 0, 0, 0);
        backtrack(0, 1, 1, 0);

        maxBishops = maxBlackBishops + maxWhiteBishops;
        System.out.println(maxBishops);
    }

    static void initializeColorPattern() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                colorPattern[i][j] = (i + j) % 2;
            }
        }
    }

    static void backtrack(int y, int x, int color, int count) {
        if (y >= N) {
            if (color == 0) {
                maxBlackBishops = Math.max(maxBlackBishops, count);
            } else {
                maxWhiteBishops = Math.max(maxWhiteBishops, count);
            }
            return;
        }

        int nextX = x + 2;
        int nextY = y;

        if (nextX >= N) {
            nextY++;
            if (nextY < N) {
                nextX = colorPattern[nextY][0] == color ? 0 : 1;
            }
        }

        if (board[y][x] == 0) {
            backtrack(nextY, nextX, color, count);
            return;
        }

        if (isValidPosition(y, x)) {
            occupied[y][x] = true;
            backtrack(nextY, nextX, color, count + 1);
            occupied[y][x] = false;
        }

        backtrack(nextY, nextX, color, count);
    }

    static boolean isValidPosition(int x, int y) {
        int directionCount = 0;
        for (int dir = 0; dir < 4; dir++) {
            if (canMove(x, y, dir)) directionCount++;
        }
        return directionCount == 4;
    }

    static boolean canMove(int x, int y, int dir) {
        int nx = x;
        int ny = y;
        while (true) {
            nx += directions[dir][0];
            ny += directions[dir][1];
            if (nx < 0 || nx >= N || ny < 0 || ny >= N) break;
            if (occupied[nx][ny]) return false;
        }
        return true;
    }
}

profile
μ–Έμ  κ°€ λ‚΄ μ½”λ“œλ‘œ 세상에 κΈ°μ—¬ν•  수 μžˆλ„λ‘, BE&Data Science 개발 기둝 λ…ΈνŠΈβ˜˜οΈ

0개의 λŒ“κΈ€

κ΄€λ ¨ μ±„μš© 정보