[백준] 14502번 연구소 - Java, 자바

Kim Ji Eun·2022년 3월 25일
2

난이도

골드 5

문제

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

풀이

  1. 백트래킹(DFS) 방법으로 벽세우기 - 완전탐색
  2. 바이러스 퍼뜨리기 - BFS
  3. 0의 개수 세기
  4. 0의 개수 최댓값 출력하기

코드

package 그래프탐색;

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

public class BOJ14502 {
    public static int n, m;
    public static int[] dx = {-1, 1, 0, 0};
    public static int[] dy = {0, 0, -1, 1};
    public static int[][] map;
    public static int[][] virusMap;
    public static int max = Integer.MIN_VALUE;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        map = new int[n][m];

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < m; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        dfs(0);

        System.out.println(max);
    }

    // 벽세우기=완탐=백트래킹
    public static void dfs(int wall) {
        if (wall == 3) {
            bfs();
            return;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 0) {
                    map[i][j] = 1;
                    dfs(wall + 1);
                    map[i][j] = 0;
                }
            }
        }
    }

    // 바이러스 퍼뜨리기
    public static void bfs() {
        Queue<int[]> q = new LinkedList<>();
        virusMap = new int[n][m];
        // virusMap 만드는 이유
        // 벽이 세개 세워질 때마다 bfs를 실행해야하므로
        // 벽이 세워진 map을 복사해서 써야함 (초기화 용도)
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                virusMap[i][j] = map[i][j];
                if (virusMap[i][j] == 2) {
                    q.add(new int[]{i, j});
                }
            }
        }

		// // map 복제
		//int[][] virusMap = new int[n][m];
		//for (int i = 0; i < n; i++) {
		//	virusMap[i] = Arrays.copyOf(map[i], m);
		//}
        
        while (!q.isEmpty()) {
            int[] tmp = q.poll();
            int x = tmp[0];
            int y = tmp[1];

            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];

                if (nx >= 0 && nx < n && ny >= 0 && ny < m && virusMap[nx][ny] == 0) {
                    virusMap[nx][ny] = 2;
                    q.add(new int[]{nx, ny});
                }
            }
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (virusMap[i][j] == 0) {
                    count++;
                }
            }
        }
        max = Math.max(max, count);


    }
}
profile
Back-End Developer

0개의 댓글