카테고리 없음

백준 14502 - 연구소 (java)

jin_j_i_n 2021. 4. 14. 13:57

www.acmicpc.net/status?user_id=dkwlsfk22&problem_id=14502&from_mine=1

 

채점 현황

 

www.acmicpc.net

백트래킹 + bfs로 풀었다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.*;
import java.util.*;
 
public class B14502 {
    static int[] dx = { 0-10+1 };
    static int[] dy = { -10+10 };
 
    public static class Point {
        int x;
        int y;
 
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    static int N; static int M ;
    static Point[] list = new Point[3];
    static int max = 0;
 
    static int[][] room;
    public static void backTracking(int cnt) {
    // System.out.println("cnt : "+cnt);
        if ( cnt == 3 ) {
            max = Math.max(bfs(), max) ;
            return;
        }else {
            for(int i=0 ; i<N ; i++) {
                for(int j=0 ; j<M; j++) {
                    // System.out.println("i : "+i+" , j: "+j);
                    if(room[i][j] == 0) {                        
                        room[i][j] = 1;
                        backTracking(cnt+1);
                        room[i][j] = 0;
                    }
                }
            }
        }
        return;
    }
 
    public static int bfs() {
        Queue<Point> que = new LinkedList<Point>();
        boolean[][] visit = new boolean[N][M];
        int[][] count = new int[N][M];        
        for(int i=0 ; i<N ; i++) {
            for(int j =0 ; j<M; j++) {
                count[i][j] = -1;
            }
        }
 
        for (int i = 0; i < N; i++) {            
            for (int j = 0; j < M; j++) {
                if (room[i][j] == 2) {
                    que.add(new Point(i, j));
                    visit[i][j] = true;
                }
            }
        }
 
        while(!que.isEmpty()) {
            Point tmp = que.poll();
            visit[tmp.x][tmp.y] = true;
            for(int i=0; i<4; i++) {
                int nx = tmp.x + dx[i];
                int ny = tmp.y + dy[i];
                if(nx >= 0 && nx < N && ny >=0 && ny < M && !visit[nx][ny] && room[nx][ny] != 1) {
                    count[nx][ny] = count[tmp.x][tmp.y] + 1;
                    que.add(new Point(nx, ny));
                }
            }
        }
        int safe = 0;
 
        for(int i=0 ; i<N; i++) {
            for(int j=0; j<M ; j++) {
                //바이러스가 아니고 , 벽이 아니어야 함
                if(room[i][j] != 1 && count[i][j] == -1 && room[i][j] != 2) {
                    safe += 1;
                }
            }
        }
        // System.out.println("safe : "+safe);
        return safe;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] inputs = br.readLine().split(" ");
        N = Integer.parseInt(inputs[0]);
        M = Integer.parseInt(inputs[1]);
 
        room = new int[N][M];
        boolean[][] visit = new boolean[N][M];
        
 
        for (int i = 0; i < N; i++) {
            inputs = br.readLine().split(" ");
            for (int j = 0; j < M; j++) {
                room[i][j] = Integer.parseInt(inputs[j]);
            }
        }
 
        backTracking(0);
        System.out.println(max);
    }
}
 
cs