# 유형 : 탐색(BFS+DFS)

# 난이도 : 골드 4

# 주어진 바이러스 중 m개를 선택하는 것을 DFS 로, 선택한 M개의 바이러스를 퍼뜨리는 것을 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package bj;
 
import java.awt.Point;
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 p17142 {
    static int moveX[] = {0,1,0,-1};
    static int moveY[] = {-1,0,1,0};
    static int N,M,minValue = Integer.MAX_VALUE;
    static int arr[][];
    static ArrayList<Point> arrList = new ArrayList<>();
    static ArrayList<Integer> tmpList = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        
        arr = new int[N+1][N+1];
        for(int i=1; i<=N; i++) {
            st = new StringTokenizer(br.readLine());
            for(int j=1; j<=N; j++) {
                //0은 빈 칸, 1은 벽, 2는 바이러스의 위치이다.
                arr[i][j] = Integer.parseInt(st.nextToken());
                if(arr[i][j] == 2) {
                    arrList.add(new Point(j,i));
                }
            }
        }
        for(int i=0; i<arrList.size(); i++) {
            tmpList = new ArrayList<>();
            tmpList.add(i);
            comb(i);
        }
        
        if(minValue == Integer.MAX_VALUE)
            System.out.println(-1);
        else
            System.out.println(minValue);
        
    }
    private static void comb(int index) {
        if(tmpList.size() == M) {
            int result = bfs();
            if(result != -1 && minValue > result)
                minValue = result;
        }else {
            for(int v=index+1; v<arrList.size(); v++) {
                tmpList.add(v);
                comb(v);
                tmpList.remove(tmpList.size()-1);
            }
        }
    }
    public static boolean isPossible(int cp[][]) {
        for(int i=1; i<=N; i++) {
            for(int j=1; j<=N; j++) {
                if(cp[i][j] == 0)
                    return false;
            }
        }
        return true;
    }
    private static int bfs() {
        // TODO Auto-generated method stub
        Queue<Point> queue = new LinkedList<Point>();
        
        for(int i=0; i<M; i++) {
            int index = tmpList.get(i);
            queue.add(new Point(arrList.get(index).x , arrList.get(index).y));
        }
        int result=0;
        int temp=0;
        int cpArr[][] = new int[N+1][N+1];
        boolean visit[][] = new boolean[N+1][N+1];
        for(int i=1; i<=N; i++) {
            cpArr[i] = arr[i].clone();
        }
        
        while(!queue.isEmpty()) {
            int size = queue.size();
            boolean near = false;
            for(int i=0; i<size; i++) {
                Point po = queue.poll();
                int x = po.x;
                int y = po.y;
                
                visit[y][x] = true;
                
                for(int d=0; d<4; d++) {
                    int newX = x + moveX[d];
                    int newY = y + moveY[d];
                    
                    if(1<=newX && newX<=&& 1<=newY && newY<=N) {
                        if(!visit[newY][newX]) {
                            visit[newY][newX] = true;
                            if(arr[newY][newX] == 0) {
                                near = true;
                                cpArr[newY][newX] = 3;
                                queue.add(new Point(newX, newY));
                            }
                            if(arr[newY][newX] == 2) {
                                queue.add(new Point(newX, newY));
                            }
                        }
                    }
                }
            }
            
            if(!near) {
                temp++;
            }else {
                result += ++temp;
                temp = 0;
            }
        }
        
        if(!isPossible(cpArr))
            return -1;
        
        
        return result;
    }
}
 
cs

+ Recent posts