|
| 1 | +```java |
| 2 | +import java.awt.Point; |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayDeque; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Iterator; |
| 12 | +import java.util.LinkedList;import java.util.Map; |
| 13 | +import java.util.StringTokenizer; |
| 14 | + |
| 15 | +public class B15683 { |
| 16 | + static class CCTV{ |
| 17 | + int id; |
| 18 | + Point p; |
| 19 | + public CCTV(int id, Point p) { |
| 20 | + this.id = id; |
| 21 | + this.p = p; |
| 22 | + } |
| 23 | + } |
| 24 | + private static BufferedReader br; |
| 25 | + private static int m; |
| 26 | + private static int n; |
| 27 | + private static LinkedList<int[]> combinations; |
| 28 | + private static int[] comb; |
| 29 | + private static ArrayList<B15683.CCTV> cctvs; |
| 30 | + private static HashSet<Point> walls; |
| 31 | + private static ArrayDeque<Point> buffer; |
| 32 | + private static int[][] drdcs = { |
| 33 | + {0, 1},{1, 0},{0, -1}, {-1, 0} |
| 34 | + }; |
| 35 | + private static int[][][] indexesDrdcsAtId = {null, {{0}, {1}, {2}, {3}} |
| 36 | + ,{{0, 2}, {1, 3}, {0, 2}, {1, 3}} |
| 37 | + ,{{0, 1}, {1, 2}, {2, 3}, {3, 0}} |
| 38 | + ,{{0, 1, 2}, {1, 2, 3}, {2, 3, 0}, {3, 0, 1}} |
| 39 | + ,{{0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}}}; |
| 40 | + private static int[][] map; |
| 41 | + public static void main(String[] args) throws IOException { |
| 42 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 43 | + var tokens = br.readLine().split(" "); |
| 44 | + n = Integer.parseInt(tokens[0]); |
| 45 | + m = Integer.parseInt(tokens[1]); |
| 46 | + walls = new HashSet<Point>(); |
| 47 | + cctvs = new ArrayList<CCTV>(); |
| 48 | + map = new int[n][m]; |
| 49 | + for(int r = 0; r<n; r++) { |
| 50 | + var st = new StringTokenizer(br.readLine()); |
| 51 | + for(int c = 0; c<m; c++) { |
| 52 | + String value = st.nextToken(); |
| 53 | + int valueInt = Integer.parseInt(value); |
| 54 | + map[r][c] = valueInt; |
| 55 | + if("0".equals(value)) |
| 56 | + continue; |
| 57 | + if("6".equals(value)) { |
| 58 | + walls.add(new Point(r, c)); |
| 59 | + }else { |
| 60 | + cctvs.add(new CCTV(valueInt, new Point(r, c))); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + int nOfZero = n*m - walls.size() - cctvs.size(); |
| 66 | + int answer = nOfZero; |
| 67 | + for (Iterator iterator = getCombinations().iterator(); iterator.hasNext();) { |
| 68 | + buffer = new ArrayDeque<Point>(); |
| 69 | + int[] directions = (int[]) iterator.next(); |
| 70 | + |
| 71 | + for(int i = 0; i<directions.length; i++) { |
| 72 | + int direction = directions[i]; |
| 73 | + //cctvs[i]에서의 direction을 기준으로 map반영 |
| 74 | + fetchMap(cctvs.get(i), direction); |
| 75 | + //printMap(); |
| 76 | + } |
| 77 | + answer = Math.min(answer, nOfZero - buffer.size()); |
| 78 | + recoverMap(); |
| 79 | + } |
| 80 | + System.out.println(answer); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + private static void printMap() { |
| 85 | + for (int r = 0; r < map.length; r++) { |
| 86 | + for (int c = 0; c < map[0].length; c++) { |
| 87 | + System.out.print(map[r][c] + " "); |
| 88 | + } |
| 89 | + System.out.println(); |
| 90 | + } |
| 91 | + System.out.println("------"); |
| 92 | + } |
| 93 | + |
| 94 | + private static void recoverMap() { |
| 95 | + while(!buffer.isEmpty()) { |
| 96 | + Point p = buffer.pollLast(); |
| 97 | + map[p.x][p.y] = 0; |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + private static void fetchMap(CCTV cctv, int direction) { |
| 103 | + for(int idDrdcs : indexesDrdcsAtId[cctv.id][direction]){ |
| 104 | + int[] drdc = drdcs[idDrdcs]; |
| 105 | + int nr = cctv.p.x; |
| 106 | + int nc = cctv.p.y; |
| 107 | + |
| 108 | + while(true) { |
| 109 | + nr+= drdc[0]; |
| 110 | + nc+= drdc[1]; |
| 111 | + if((nr<0 || nr >= n || nc <0 || nc>=m) || walls.contains(new Point(nr, nc))) { |
| 112 | + break; |
| 113 | + } |
| 114 | + if(map[nr][nc] >= 1 && map[nr][nc] <= 5) |
| 115 | + continue; |
| 116 | + map[nr][nc] = cctv.id; |
| 117 | + buffer.addLast(new Point(nr, nc)); |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static LinkedList<int[]> getCombinations() { |
| 124 | + combinations = new LinkedList<int[]>(); |
| 125 | + comb = new int[cctvs.size()]; |
| 126 | + recur(0); |
| 127 | + |
| 128 | + return combinations; |
| 129 | + } |
| 130 | + |
| 131 | + private static void recur(int index) { |
| 132 | + if(index == cctvs.size()) { |
| 133 | + combinations.add(comb.clone()); |
| 134 | + return; |
| 135 | + } |
| 136 | + for(int i = 0; i<4; i++) { |
| 137 | + comb[index] = i; |
| 138 | + recur(index+1); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | +``` |
0 commit comments