๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][๋ฐฑ์ค€] 14502 ์—ฐ๊ตฌ์†Œ

๋ฐ˜์‘ํ˜•

[๋ฐฑ์ค€] 14502 ์—ฐ๊ตฌ์†Œ

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

๋ฌธ์ œ์ ‘๊ทผ

๋ชจ๋“  ๋งต์„ ๋Œ๋ฉด์„œ ๋ฒฝ์„ 3๊ฐœ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•ด์•ผํ•œ๋‹ค.
์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋ฒฝ์„ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ์ง€์ ์—์„œ ๋ฒฝ์„ ์„ธ์šฐ๋Š” ๊ฒฝ์šฐ์™€ ์„ธ์šฐ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•œ๋‹ค.
ํฌ๊ฒŒ๋ณด๋ฉด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค,
1.๋ฒฝ์„ 3๊ฐœ ์„ธ์šด๋‹ค
2.๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ ค ๋ณธ๋‹ค.
3.์•ˆ์ „ ์˜์—ญ์„ ์„ผ๋‹ค.
4.์ด๋ฒˆ ๊ฒฝ์šฐ๊ฐ€ ์ตœ๋Œ€ ์•ˆ์ „ ์˜์—ญ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
5.1~4๋ฅผ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.

Code

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

public class BOJ14502 {
    static int n,m;
    static int[][] map;
    static int maxCnt = 0;
    static int[] dx = {-1,1,0,0};
    static int[] dy = {0,0,-1,1};

    static class XY {
        int x;
        int y;
        XY(int x, int y){
            this.x = x;
            this.y = y;
        }
    }

    /**
     * ๋ฒฝ์„ ์„ธ์šฐ๋Š” ํ•จ์ˆ˜
     * 1. ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ map์ด 0์ธ ๊ณณ์—์„œ ๋ฒฝ์„ ์„ธ์šฐ๋Š” ๊ฒฝ์šฐ์™€ ์•ˆ์„ธ์šฐ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋„๋ก ํ•œ๋‹ค.
     * 2. ์„ธ์šด ๋ฒฝ์ด 3๊ฐœ๊ฐ€ ๋œ๋‹ค๋ฉด ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆฌ๊ณ  ์•ˆ์ „ ์˜์—ญ์„ ์นด์šดํŠธํ•œ๋‹ค.
     * 3. ๋” ๋งŽ์€ ์•ˆ์ „ ์˜์—ญ์„ ์ €์žฅํ•œ๋‹ค. 
     */
    public static void setWall(int cnt){
        if(cnt == 3){
            int safeCnt = spreadVirus();
            maxCnt = Math.max(maxCnt, safeCnt);
        } else {
            for(int i = 0; i<n; i++){
                for(int j = 0; j<m; j++){
                    if(map[i][j] == 0){
                        // ๋ฒฝ์„ ์„ธ์šฐ๋Š” ๊ฒฝ์šฐ
                        map[i][j] = 1;
                        setWall(cnt + 1);
                        // ๋ฒฝ์„ ์„ธ์šฐ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
                        map[i][j] = 0;
                    }
                }
            }
        }
    }

    /**
     * ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆฌ๋Š” ํ•จ์ˆ˜
     * bfs๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆฐ๋‹ค.
     * ํ์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ง€์ ์„ ์ €์žฅํ•˜๊ณ ,
     * ํ•ด๋‹น ์ง€์ ์—์„œ ์ƒํ•˜์ขŒ์šฐ๋กœ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆด์ˆ˜ ์žˆ๋Š” ์•ˆ์ „์˜์—ญ์„ ํƒ์ƒ‰ํ•˜๊ณ ,
     * ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆฐ๋‹ค.
     * ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์˜์—ญ์— ํผ๋œจ๋ฆฌ๊ณ  ์•ˆ์ „์˜์—ญ์„ ์นด์šดํŠธํ•˜๊ณ  ๋ฆฌํ„ดํ•ด์ค€๋‹ค.
     */
    public static int spreadVirus(){
        Queue<XY> q = new LinkedList<>();
        int[][] visited = new int[n][m];  // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
        int[][] tempMap = new int[n][m];  // ๊ธฐ์กด ๋งต์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฐ์—ด
        // ๋งต์„ ๋ณต์‚ฌํ•œ๋‹ค.
        for(int i = 0; i<n; i++)
            for(int j = 0; j<m; j++)
                tempMap[i][j] = map[i][j];
        for(int i = 0; i<n; i++){
            for(int j = 0; j<m; j++){
                // ๋ชจ๋“  map์„ ๋Œ๋ฉด์„œ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆฐ๋‹ค.
                if(tempMap[i][j] == 2 && visited[i][j] == 0){
                    // ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ง€์—ญ์„ ๋ฐฉ๋ฌธํ•œ๋‹ค.
                    q.add(new XY(i,j));
                    while(!q.isEmpty()){
                        XY cur = q.poll();
                        visited[cur.x][cur.y] = 1;
                        for(int k = 0; k<4; k++){
                            // ์ƒํ•˜์ขŒ์šฐ๋กœ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆด ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ ํ›„, ํผ๋œจ๋ฆด ์ˆ˜ ์žˆ๋‹ค๋ฉด ํผ๋œจ๋ฆฐ๋‹ค.
                            int next_x = cur.x + dx[k];
                            int next_y = cur.y + dy[k];
                            if(!isIn(next_x,next_y)) continue;
                            if(tempMap[next_x][next_y] != 0 || visited[next_x][next_y] == 1) continue;
                            q.add(new XY(next_x,next_y));
                            tempMap[next_x][next_y] = 2;
                        }
                    }
                }
            }
        }
        return countSafe(tempMap);
    }

    /**
     * ์•ˆ์ „ ์˜์—ญ์„ ์นด์šดํŠธ ํ•œ๋‹ค. 
     * ๋งต์„ ๋Œ๋ฉด์„œ ์•ˆ์ „์˜์—ญ์„ ์นด์šดํŠธํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค.
     */
    public static int countSafe(int[][] map) {
        int cnt = 0;
        for(int i = 0; i<n; i++) {
            for (int j = 0; j < m; j++) {
                if(map[i][j] == 0) cnt++;
            }
        }
        return cnt;
    }

    /**
     * map์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
     */
    public static boolean isIn(int x, int y){
        if(x >= 0 && x < n && y >= 0 && y < m) return true;
        else return false;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        n = Integer.parseInt(input[0]);
        m = Integer.parseInt(input[1]);
        map = new int[n][m];
        for(int i = 0; i<n; i++){
            input = br.readLine().split(" ");
            for(int j = 0; j<m; j++){
                map[i][j] = Integer.parseInt(input[j]);
            }
        }
        setWall(0);
        System.out.println(maxCnt);
    }
}

์–ด๋ ค์› ๋˜ ์  / ๋ฐฐ์šด ์  / ๋Š๋‚€ ์ 

๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์•„์ง๋„ ์žฌ๊ท€ํ•จ์ˆ˜๋Š” ๋„ˆ๋ฌด ์‹ ๊ธฐ?ํ•˜๋‹ค.

// ๋ฒฝ์„ ์„ธ์šฐ๋Š” ๊ฒฝ์šฐ
map[i][j] = 1;
setWall(cnt + 1);
// ๋ฒฝ์„ ์„ธ์šฐ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
map[i][j] = 0;

ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด map[i][j] = 1(๋ฒฝ์€ ์„ธ์šด ์ƒํƒœ)์ด ๋˜๊ณ  cnt+1๋œ์ฑ„๋กœ setWallํ•จ์ˆ˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹คํ–‰๋˜๊ฒŒ ๋˜๊ณ , ๊ธฐ์กด ์ง„ํ–‰๋˜๊ณ  ์žˆ๋˜ setWallํ•จ์ˆ˜๋Š” cnt๊ฐ€ ๊ทธ๋Œ€๋กœ์ธ ์ƒํƒœ๋กœ map[i][j] = 0 (๋ฒฝ์„ ์„ธ์šฐ์ง€ ์•Š์€ ์ƒํƒœ)๋กœ ๋‹ค์Œ ์ง€์—ญ์œผ๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค.
์ด๋Ÿฌํ•œ ์‹์œผ๋กœ ๋ฒฝ์„ 3๊ฐœ ์„ธ์šฐ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š”๊ฒƒ์ด๋‹ค.

๋ฐ˜์‘ํ˜•