๊ธฐ๋ก๋ฐฉ

BOJ_17141 : ์—ฐ๊ตฌ์†Œ 2 ๋ณธ๋ฌธ

CodingTest/Java

BOJ_17141 : ์—ฐ๊ตฌ์†Œ 2

Soom_1n 2023. 4. 6. 16:48

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

 

17141๋ฒˆ: ์—ฐ๊ตฌ์†Œ 2

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์— ์Šน์›์ด๊ฐ€ ์นจ์ž…ํ–ˆ๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์œ ์ถœํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์Šน์›์ด๋Š” ์—ฐ๊ตฌ์†Œ์˜ ํŠน์ • ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค M๊ฐœ๋ฅผ ๋†“์„ ๊ฒƒ์ด๊ณ , ์Šน์›์ด์˜ ์‹ ํ˜ธ์™€ ๋™์‹œ์— ๋ฐ”์ด

www.acmicpc.net



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

  • N x N ์—ฐ๊ตฌ์†Œ์—์„œ M๊ฐœ์˜ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋ชจ๋‘ ํผ์ง€๋Š” ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.
    • ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๊ณณ : 2 , ๋นˆ ๊ณต๊ฐ„ : 0, ๋ฒฝ : 1
    • 2์˜ ๊ฐœ์ˆ˜๋Š” 10 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.
    • ๋ชจ๋‘ ๋†“์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๋†“์„ ์ˆ˜ ์žˆ๋Š” 2๊ฐ€ ์ ํžŒ ์ž๋ฆฌ๋“ค ์ค‘ M๊ฐœ๋ฅผ ๋ฝ‘์•„์•ผ ํ•˜๋ฏ€๋กœ ์กฐํ•ฉ์ด๋‹ค.
  • ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง€๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ BFS๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    private static class Point {
        int r, c;

        public Point(int r, int c) {
            this.r = r;
            this.c = c;
        }
    }

    private static int N, M, ans, selected[];
    private static int[][] map, temp;
    private static boolean[][] visited;
    private static ArrayList<Point> virus;
    private static int[] dx = {-1, 0, 1, 0};
    private static int[] dy = {0, 1, 0, -1};

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        map = new int[N][N];
        temp = new int[N][N];
        selected = new int[M];
        virus = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                int num = Integer.parseInt(st.nextToken());
                if (num == 1) map[i][j] = -1;
                else if (num == 2) {
                    virus.add(new Point(i, j));
                    map[i][j] = 0;
                } else {
                    map[i][j] = 0;
                }
            }
        }
        // Combination
        ans = Integer.MAX_VALUE;
        comb(0, 0);

        // Output
        System.out.println(ans == Integer.MAX_VALUE ? -1 : ans);
    }

    private static void comb(int start, int cnt) {
        if (cnt == M) {
            deepCopy();
            visited = new boolean[N][N];
            Queue<Point> que = new LinkedList<>();
            for (int i = 0; i < M; i++) {
                Point p = virus.get(selected[i]);
                que.add(p);
                visited[p.r][p.c] = true;
            }
            bfs(que, visited);
            return;
        }

        for (int i = start; i <= virus.size() - (M - cnt); i++) {
            selected[cnt] = i;
            comb(i + 1, cnt + 1);
        }
    }

    private static void bfs(Queue<Point> que, boolean[][] visited) {
        int time = 0;
        while (!que.isEmpty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                Point point = que.poll();
                temp[point.r][point.c] = time;
                for (int j = 0; j < dx.length; j++) {
                    int r = point.r + dx[j];
                    int c = point.c + dy[j];
                    if (0 <= r && r < N && 0 <= c && c < N && !visited[r][c] && temp[r][c] == 0) {
                        visited[r][c] = true;
                        que.add(new Point(r, c));
                    }
                }
            }
            if (que.isEmpty()) break;
            time++;
        }
        
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (temp[i][j] == 0 && !visited[i][j]) return;
            }
        }

        ans = Math.min(ans, time);
    }

    private static void deepCopy() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                temp[i][j] = map[i][j];
            }
        }
    }
}

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

  • ๋งต ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ, 2๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ArrayList virus์— ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ด๋‘๊ณ  ๋งต์—๋Š” 0์œผ๋กœ ์ ์–ด๋‘”๋‹ค.
  • ์กฐํ•ฉ์—์„œ virus์—์„œ ์–ด๋–ค ์ขŒํ‘œ๋ฅผ M๊ฐœ ๊ณ ๋ฅผ์ง€ ์ธ๋ฑ์Šค๋ฅผ ์„ ํƒํ•œ๋‹ค.
  • ๊ณ ๋ฅธ ์œ„์น˜์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๋ชจ๋‘ ํผํŠธ๋ ค๋ณด๊ณ  0์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•œ ๋’ค ans๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
    • ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผํŠธ๋ฆฌ๋Š” ๊ฑด ์›๋ณธ ๋งต์„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  temp๋กœ ๊นŠ์€ ๋ณต์‚ฌ ํ›„ ์‹คํ–‰ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ฒฝ์„ ๊ตณ์ด 1์—์„œ -1๋กœ ๋ฐ”๊ฟ€ ํ•„์š”๋Š” ์—†์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

728x90

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

BOJ_13459 : ๊ตฌ์Šฌ ํƒˆ์ถœ  (0) 2023.04.06
BOJ_2239 : ์Šค๋„์ฟ   (0) 2023.04.06
BOJ_2636 : ์น˜์ฆˆ  (0) 2023.04.06
BOJ_1600 : ๋ง์ด ๋˜๊ณ ํ”ˆ ์›์ˆญ์ด  (0) 2023.04.06
BOJ_10971 : ์™ธํŒ์› ์ˆœํšŒ 2  (0) 2023.04.06