λšœλΆ€λ‹ˆμ˜ Devlog

[BOJ] 2615.였λͺ© (Java)

by λšœλΆ€λ‹ˆ

 

 

2615번: 였λͺ©

였λͺ©μ€ λ°”λ‘‘νŒμ— 검은 λ°”λ‘‘μ•Œκ³Ό 흰 λ°”λ‘‘μ•Œμ„ κ΅λŒ€λ‘œ λ†“μ•„μ„œ κ²¨λ£¨λŠ” κ²Œμž„μ΄λ‹€. λ°”λ‘‘νŒμ—λŠ” 19개의 κ°€λ‘œμ€„κ³Ό 19개의 μ„Έλ‘œμ€„μ΄ κ·Έλ €μ Έ μžˆλŠ”λ° κ°€λ‘œμ€„μ€ μœ„μ—μ„œλΆ€ν„° μ•„λž˜λ‘œ 1번, 2번, ... ,19번의 번호

www.acmicpc.net

 

λ°”λ‘‘νŒμ— 19개의 κ°€λ‘œμ€„κ³Ό 19개의 μ„Έλ‘œμ€„μ΄ κ·Έλ €μ Έ μžˆμ„ λ•Œ, 같은 μƒ‰μ˜ λ°”λ‘‘μ•Œμ΄ μ—°μ†μ μœΌλ‘œ λ‹€μ„― μ•Œ 놓이면 이기게 λ˜λŠ” κ²Œμž„μž…λ‹ˆλ‹€.

μ—¬κΈ°μ„œ μ—°μ†μ μœΌλ‘œλŠ” κ°€λ‘œ, μ„Έλ‘œ λ˜λŠ” λŒ€κ°μ„  λ°©ν–₯을 λœ»ν•˜λ©°, μ—¬μ„― μ•Œ 이상이 μ—°μ†μ μœΌλ‘œ 놓은 κ²½μš°μ—λŠ” 이긴 것이 μ•„λ‹™λ‹ˆλ‹€.

λ°”λ‘‘νŒ μƒνƒœκ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, 검은색은 1, 흰색은 2, μ•Œμ΄ 놓이지 μ•Šμ€ μžλ¦¬λŠ” 0으둜 ν‘œμ‹œν•©λ‹ˆλ‹€.

검은색이 이기면 1, 흰색이 이기면 2, 아직 μŠΉλΆ€κ°€ κ²°μ •λ˜μ§€ μ•ŠμœΌλ©΄ 0을 좜λ ₯ν•©λ‹ˆλ‹€.

그리고 검은색 λ˜λŠ” 흰색이 이겼을 경우, λ‘˜μ§Έ 쀄에 μ—°μ†λœ λ‹€μ„― 개의 λ°”λ‘‘μ•Œ 쀑 κ°€μž₯ μ™Όμͺ½μ— μžˆλŠ” λ°”λ‘‘μ•Œμ˜ κ°€λ‘œμ€„, μ„Έλ‘œμ€„ 번호λ₯Ό μˆœμ„œλŒ€λ‘œ 좜λ ₯ν•©λ‹ˆλ‹€. μ—°μ†λœ λ‹€μ„― 개의 λ°”λ‘‘μ•Œμ΄ μ„Έλ‘œλ‘œ 놓인 경우, κ·Έ 쀑 κ°€μž₯ μœ„μ— μžˆλŠ” λ°”λ‘‘μ•Œμ˜ κ°€λ‘œμ€„, μ„Έλ‘œμ€„ 번호λ₯Ό μˆœμ„œλŒ€λ‘œ 좜λ ₯ν•©λ‹ˆλ‹€.

 

문제 μš”μ•½

1. κ°€λ‘œ, μ„Έλ‘œ λ˜λŠ” λŒ€κ°μ„  λ°©ν–₯으둜 λ°”λ‘‘μ•Œμ΄ μ—°μ†μ μœΌλ‘œ λ‹€μ„― κ°œκ°€ λ†“μ΄λŠ” 경우 μ°ΎκΈ°

2. μ—¬μ„― μ•Œ 이상이 μ—°μ†μ μœΌλ‘œ λ†“μ΄λŠ” 경우 이긴 것이 μ•„λ‹˜

3. 검은색이 이기면 1, 흰색이 이기면 2, 아직 μŠΉλΆ€κ°€ κ²°μ •λ˜μ§€ μ•ŠμœΌλ©΄ 0 좜λ ₯

4. μ—°μ†λœ λ‹€μ„― 개 λ°”λ‘‘μ•Œ 쀑 κ°€μž₯ μ™Όμͺ½μ— μžˆλŠ” λ°”λ‘‘μ•Œ μœ„μΉ˜ 좜λ ₯ (μ„Έλ‘œλ‘œ 놓이면, κ°€μž₯ μœ„μͺ½ λ°”λ‘‘μ•Œ μœ„μΉ˜ 좜λ ₯)

 

문제 풀이

처음 λ°”λ‘‘νŒμ˜ μƒνƒœλ₯Ό 뢈러올 λ•Œ, 검은색 λ°”λ‘‘μ•Œμ˜ μœ„μΉ˜μ™€ 흰색 λ°”λ‘‘μ•Œμ˜ μœ„μΉ˜λ₯Ό λ¦¬μŠ€νŠΈμ— μ €μž₯ν•΄λ‘‘λ‹ˆλ‹€.

그리고 검은색 λ°”λ‘‘μ•Œ μœ„μΉ˜ λ¦¬μŠ€νŠΈμ™€ 흰색 λ°”λ‘‘μ•Œ μœ„μΉ˜ 리슀트λ₯Ό μˆœμ„œλŒ€λ‘œ ν™•μΈν•˜λ©° λ‹€μ„― μ•Œμ΄ μ—°μ†μ μœΌλ‘œ λ†“μ΄λŠ”μ§€ ν™•μΈν•©λ‹ˆλ‹€.

μ—¬κΈ°μ„œ μ €λŠ” λ‹€μ„―μ•Œμ΄ 놓인 μƒνƒœμΌ λ•Œ, μΆœλ°œν•œ μœ„μΉ˜ μ•žμͺ½μ— λ°”λ‘‘μ•Œμ΄ μžˆλŠ”μ§€ ν™•μΈν•˜μ—¬ 6개 이상이 λ˜λŠ”μ§€ ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€.

좜발 μœ„μΉ˜μ™€ 좜발 μœ„μΉ˜ μ•žμͺ½μ— λŒ€ν•œ 이미지

 

import java.util.*;

class Position {
    int row;
    int col;

    Position(int row, int col) {
        this.row = row;
        this.col = col;
    }
}

public class Main {
    public static List<Position> solution(int[][] board, List<Position> positions, int color) {
        int[][] direction = {{0, 1}, {1, -1}, {1, 0}, {1, 1}};
        int[][] checkDirection = {{0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
        int count;
        boolean isBreak = false;
        boolean isExistence;
        List<Position> winPosition = null;

        Queue<Position> queue = new LinkedList<>();

        for (Position position : positions) {
            if (isBreak) break;

            for (int i = 0; i < 4; i++) {
                winPosition = new ArrayList<>();
                queue.add(position);
                count = 0;

                while (!queue.isEmpty()) {
                    Position pos = queue.poll();
                    winPosition.add(new Position(pos.row, pos.col));
                    count++;

                    int nextRow = pos.row + direction[i][0];
                    int nextCol = pos.col + direction[i][1];

                    if (0 <= nextRow && nextRow < 19 && 0 <= nextCol && nextCol < 19) {
                        if (board[nextRow][nextCol] == color) {
                            queue.add(new Position(nextRow, nextCol));
                        }
                    }
                }

                if (count == 5) {
                    int checkRow = position.row + checkDirection[i][0];
                    int checkCol = position.col + checkDirection[i][1];

                    isExistence = false;

                    // 였λͺ©μ΄ 된 μ‹œμž‘μ  μ•žμ— 돌이 μžˆλŠ”μ§€ ν™•μΈν•˜μ—¬ 윑λͺ© 이상인지 체크
                    if (0 <= checkRow && checkRow < 19 && 0 <= checkCol && checkCol < 19) {
                        if (board[checkRow][checkCol] == color) {
                            isExistence = true;
                        }
                    }

                    if (!isExistence) {
                        isBreak = true;
                        break;
                    }
                }
            }

        }

        return winPosition;

    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int[][] board = new int[19][19];
        List<Position> blackPos = new ArrayList<>();
        List<Position> whitePos = new ArrayList<>();

        for (int i = 0; i < 19; i++) {
            for (int j = 0; j < 19; j++) {
                int temp = input.nextInt();
                board[i][j] = temp;
                if (temp == 1) {
                    blackPos.add(new Position(i, j));
                } else if (temp == 2) {
                    whitePos.add(new Position(i, j));
                }
            }
        }

        List<Position> blackWinPosition = solution(board, blackPos, 1);
        List<Position> whiteWinPosition = solution(board, whitePos, 2);

        if (blackWinPosition != null && blackWinPosition.size() == 5) {
            System.out.println(1);
            blackWinPosition.sort(((o1, o2) -> o1.col - o2.col));
            System.out.println((blackWinPosition.get(0).row + 1) + " " + (blackWinPosition.get(0).col + 1));
        } else if (whiteWinPosition != null && whiteWinPosition.size() == 5) {
            System.out.println(2);
            whiteWinPosition.sort(((o1, o2) -> o1.col - o2.col));
            System.out.println((whiteWinPosition.get(0).row + 1) + " " + (whiteWinPosition.get(0).col + 1));
        } else {
            System.out.println(0);
        }
    }
}

 

 

'Algorithm > BOJ' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[BOJ] 2660.회μž₯ 뽑기 (Java, BFS)  (0) 2023.01.01
[BOJ] 2016.λ―ΈνŒ…μ£Όμ„ ν•˜κΈ°  (0) 2022.06.13
[BOJ] 1759.μ•”ν˜Έλ§Œλ“€κΈ° (Java)  (0) 2021.06.20
[BOJ] 12094.A와 B (Java)  (0) 2021.06.10
[BOJ] 10159.μ €μšΈ (Java)  (0) 2021.05.02

λΈ”λ‘œκ·Έμ˜ 정보

λšœλΆ€λ‹ˆμ˜ Devlog

λšœλΆ€λ‹ˆ

ν™œλ™ν•˜κΈ°