연구소

Huisu·2023년 9월 1일
0

Coding Test Practice

목록 보기
35/98
post-thumbnail

문제

14502번: 연구소

문제 설명

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다.

연구소는 크기가 N×M인 직사각형으로 나타낼 수 있으며, 직사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.

일부 칸은 바이러스가 존재하며, 이 바이러스는 상하좌우로 인접한 빈 칸으로 모두 퍼져나갈 수 있다. 새로 세울 수 있는 벽의 개수는 3개이며, 꼭 3개를 세워야 한다.

예를 들어, 아래와 같이 연구소가 생긴 경우를 살펴보자.

2 0 0 0 1 1 0
0 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

이때, 0은 빈 칸, 1은 벽, 2는 바이러스가 있는 곳이다. 아무런 벽을 세우지 않는다면, 바이러스는 모든 빈 칸으로 퍼져나갈 수 있다.

2행 1열, 1행 2열, 4행 6열에 벽을 세운다면 지도의 모양은 아래와 같아지게 된다.

2 1 0 0 1 1 0
1 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 1 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

바이러스가 퍼진 뒤의 모습은 아래와 같아진다.

2 1 0 0 1 1 2
1 0 1 0 1 2 2
0 1 1 0 1 2 2
0 1 0 0 0 1 2
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

벽을 3개 세운 뒤, 바이러스가 퍼질 수 없는 곳을 안전 영역이라고 한다. 위의 지도에서 안전 영역의 크기는 27이다.

연구소의 지도가 주어졌을 때 얻을 수 있는 안전 영역 크기의 최댓값을 구하는 프로그램을 작성하시오.

제한 사항

첫째 줄에 지도의 세로 크기 N과 가로 크기 M이 주어진다. (3 ≤ N, M ≤ 8)

둘째 줄부터 N개의 줄에 지도의 모양이 주어진다. 0은 빈 칸, 1은 벽, 2는 바이러스가 있는 위치이다. 2의 개수는 2보다 크거나 같고, 10보다 작거나 같은 자연수이다.

빈 칸의 개수는 3개 이상이다.

입출력 예

입력출력
7 7
2 0 0 0 1 1 0
0 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 027
4 6
0 0 0 0 0 0
1 0 0 0 0 2
1 1 1 0 0 2
0 0 0 0 0 29
8 8
2 0 0 0 0 0 0 2
2 0 0 0 0 0 0 2
2 0 0 0 0 0 0 2
2 0 0 0 0 0 0 2
2 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 03

입출력 예 설명

아이디어

조합 + 브루트포스 + bfs

제출 코드


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

// https://www.acmicpc.net/problem/14502
public class four14502 {
    private int[] drow = new int[] {0, 0, 1, -1};
    private int[] dcol = new int[] {1, -1, 0, 0};
    private int rowNum;
    private int colNum;
    private int[][] map;
    private List<int[]> canWall;
    public void solution() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer infoToken = new StringTokenizer(reader.readLine());
        rowNum = Integer.parseInt(infoToken.nextToken());
        colNum = Integer.parseInt(infoToken.nextToken());
        map = new int[rowNum][colNum];

        canWall = new ArrayList<>();

        for (int i = 0; i < rowNum; i++) {
            StringTokenizer mapToken = new StringTokenizer(reader.readLine());
            for (int j = 0; j < colNum; j++) {
                map[i][j] = Integer.parseInt(mapToken.nextToken());
                if (map[i][j] == 0) canWall.add(new int[] {i, j});
            }
        }

        int wallSize = canWall.size();
        int maxSafe = 0;

        int[] makeWall = new int[3];

        for (int i = 0; i < wallSize - 2; i++) {
            makeWall[0] = i;
            for (int j = i + 1; j < wallSize - 1; j++) {
                makeWall[1] = j;
                for (int k = j + 1; k < wallSize; k++) {
                    makeWall[2] = k;
                    maxSafe = Math.max(maxSafe, bfs(makeWall));
                }
            }
        }
        System.out.println(maxSafe);
    }

    private int bfs(int[] makeWall) {
        int[][] copyMap = new int[rowNum][colNum];
        Queue<int[]> virus = new LinkedList<>();
        for (int i = 0; i < rowNum; i++) {
            for (int j = 0; j < colNum; j++) {
                copyMap[i][j] = map[i][j];
                if (copyMap[i][j] == 2) virus.add(new int[] {i, j});
            }
        }
        boolean[][] visited = new boolean[rowNum][colNum];
        int safeZone = 0;
        // 벽 만들기
        for (int wall : makeWall) {
            int[] position = canWall.get(wall);
            copyMap[position[0]][position[1]] = 1;
        }

        while(!virus.isEmpty()) {
            int[] current = virus.poll();
            visited[current[0]][current[1]] = true;
            for (int i = 0; i < 4; i++) {
                int nextRow = current[0] + drow[i];
                int nextCol = current[1] + dcol[i];
                if (
                        -1 < nextRow && nextRow < rowNum &&
                        -1 < nextCol && nextCol < colNum &&
                        !visited[nextRow][nextCol] &&
                        copyMap[nextRow][nextCol] != 1
                ) {
                    if (copyMap[nextRow][nextCol] == 0)
                        copyMap[nextRow][nextCol] = 2;
                    virus.offer(new int[] {nextRow, nextCol});
                }
            }
        }

        for (int i = 0; i < rowNum; i++) {
            for (int j = 0; j < colNum; j++) {
                if (copyMap[i][j] == 0) safeZone++;
            }
        }
        return safeZone;
    }

    public static void main(String[] args) throws IOException {
        new four14502().solution();
    }
}

0개의 댓글