From af346caff36a1d101685f33931e8b97e06bd3844 Mon Sep 17 00:00:00 2001 From: yess98 <52556037+yess98@users.noreply.github.com> Date: Thu, 18 Nov 2021 12:48:15 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=96=91=EC=9D=80=EC=84=9C]=20=EC=97=B0?= =?UTF-8?q?=EA=B5=AC=EC=86=8C3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\226\221\354\235\200\354\204\234.java" | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 "solution/BOJ_17142_\354\227\260\352\265\254\354\206\214-3/\354\226\221\354\235\200\354\204\234.java" diff --git "a/solution/BOJ_17142_\354\227\260\352\265\254\354\206\214-3/\354\226\221\354\235\200\354\204\234.java" "b/solution/BOJ_17142_\354\227\260\352\265\254\354\206\214-3/\354\226\221\354\235\200\354\204\234.java" new file mode 100644 index 0000000..2334ffe --- /dev/null +++ "b/solution/BOJ_17142_\354\227\260\352\265\254\354\206\214-3/\354\226\221\354\235\200\354\204\234.java" @@ -0,0 +1,149 @@ +``` +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Main{ + static int n ; + static int m ; + static int [][] map; + static int [][] checkmap; + static int wallcount = 0; + static int viruscount = 0 ; + static LinkedList virus_candidate = new LinkedList < int [] > (); + static ArrayList < int [] > wall_position = new ArrayList < int [] >(); + static boolean [] virus_candidate_check; + static boolean [][] check ; + static int [] dx = {1,-1,0,0}; + static int [] dy = {0,0,1,-1}; + static int available; + static Queue queue = new ArrayDeque (); + static int time; + static int answer = Integer.MAX_VALUE ; + static boolean flag = false; + static int blankcount = 0 ; + + 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()); + map = new int [n][n]; + for(int i = 0 ; i < n ; i ++) { + st = new StringTokenizer(br.readLine()); + for(int j = 0 ; j < n ; j ++) { + map[i][j] = Integer.parseInt(st.nextToken()); + //바이러스 설치 할 수 있는곳 저장시켜두기 + if (map[i][j] == 2) + { + virus_candidate.add(new int [] {i,j}); + viruscount ++; + } + // 벽 개수 체크 + if (map[i][j] == 1) + { + wall_position.add(new int [] {i,j}); + wallcount ++; + } + } + } + virus_candidate_check = new boolean [virus_candidate.size()]; + + // 빈칸의 갯수 = n * n - wallcount - viruscount ; + blankcount = n * n - wallcount - viruscount; + choose_position(0, 0); + + + if (flag == false) System.out.println(-1); + else System.out.println(answer); + + } + + + + static void put_virus () { + checkmap = new int[n][n]; + queue = new ArrayDeque (); + check = new boolean [n][n]; + for(int i = 0 ; i < wall_position.size(); i ++) { + int [] temp = wall_position.get(i); + checkmap[temp[0]][temp[1]] = -1 ; + } + for(int i = 0 ; i < virus_candidate_check.length; i ++) { + if (virus_candidate_check[i] == true ) { + int [] pos = virus_candidate.get(i); + checkmap[pos[0]][pos[1]] = 0; + queue.add(new int[] {pos[0],pos[1],0}); + check[pos[0]][pos[1]] = true; + } + else { + int [] pos = virus_candidate.get(i); + checkmap[pos[0]][pos[1]] = -2; + } + } + + available = 0 ; + time = 0 ; + infection(); + + } + + static void choose_position(int cnt, int idx) { + if (cnt == m ) + { + // 바이러스 넣을 부분 표시. + put_virus (); + return ; + } + for(int i = idx ; i < virus_candidate.size(); i ++) { + if (virus_candidate_check[i] == false) { + virus_candidate_check[i] = true ; + choose_position(cnt + 1, i + 1); + virus_candidate_check[i] = false ; + } + } + } + + static void infection() { + + while (!queue.isEmpty()) { + int [] now = queue.poll(); + + + for(int i = 0 ; i < dx.length; i ++) { + int nx = now[0] + dx[i]; + int ny = now[1] + dy[i]; + if (nx < 0 || ny < 0 || ny >= n || nx >= n) continue; + if (check[nx][ny] == true ) continue; + if (checkmap[nx][ny] == -1 ) continue; + if (checkmap [nx][ny] == -2 ) { + check[nx][ny] = true; + queue.add(new int [] {nx,ny,now[2] + 1}); + continue; + } + queue.add(new int [] {nx,ny,now[2] + 1}); + check[nx][ny] = true; + available ++; + time = now[2] + 1; + + } + + } + + if (available == blankcount ) { + //가능 + flag = true; + if (answer > time) { + answer = time; + } + } + } + +} + +```