|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +class Solution { |
| 5 | + static class Point{ |
| 6 | + int r; |
| 7 | + int c; |
| 8 | + Point(int r, int c){ |
| 9 | + this.r = r; |
| 10 | + this.c = c; |
| 11 | + } |
| 12 | + Point translate(int dr, int dc){ |
| 13 | + return new Point(r+dr, c+dc); |
| 14 | + } |
| 15 | + } |
| 16 | + private static int[][] Drdcs = { |
| 17 | + {0, 1}, |
| 18 | + {1, 0}, |
| 19 | + {0, -1}, |
| 20 | + {-1, 0} |
| 21 | + }; |
| 22 | + private static int R; |
| 23 | + private static int C; |
| 24 | + private static int[][] Map; |
| 25 | + private static HashMap<Integer, LinkedList<Point>> PointsOfId; |
| 26 | + public int solution(String[] storages, String[] requests) { |
| 27 | + C = storages[0].length(); |
| 28 | + R = storages.length; |
| 29 | + int answer = R*C; |
| 30 | + Map = new int[R+2][C+2]; |
| 31 | + PointsOfId = new HashMap<Integer, LinkedList<Point>>(); |
| 32 | + for(int r = 0; r < R; r++){ |
| 33 | + String storage = storages[r]; |
| 34 | + for(int c = 0; c< C; c++){ |
| 35 | + char cc = storage.charAt(c); |
| 36 | + int id = cc - 'A' + 1; |
| 37 | + var points = PointsOfId.get(id); |
| 38 | + if(points == null){ |
| 39 | + points = new LinkedList<Point>(); |
| 40 | + PointsOfId.put(id, points); |
| 41 | + } |
| 42 | + points.add(new Point(r+1, c+1)); |
| 43 | + Map[r+1][c+1] = id; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + Integer preTarget = null; |
| 48 | + for(String request: requests){ |
| 49 | + int target = request.charAt(0) - 'A'+1; |
| 50 | + if(request.length() != 1){ |
| 51 | + // for(Point p: PointsOfId.get(target)){ 런타임 에러!!!! |
| 52 | + // Map[p.r][p.c] = 0; |
| 53 | + // answer--; |
| 54 | + // } |
| 55 | + for(Point p: PointsOfId.getOrDefault(target, new LinkedList<Point>())){ |
| 56 | + Map[p.r][p.c] = 0; |
| 57 | + answer--; |
| 58 | + } |
| 59 | + continue; |
| 60 | + } |
| 61 | + var visited = new boolean[R+2][C+2]; |
| 62 | + |
| 63 | + var startP = new Point(0, 0); |
| 64 | + visit(visited, startP); |
| 65 | + var q = new ArrayDeque<Point>(); |
| 66 | + q.offer(startP); |
| 67 | + while(!q.isEmpty()){ |
| 68 | + Point p = q.poll(); |
| 69 | + for(int[] drdc: Drdcs){ |
| 70 | + var nextP = p.translate(drdc[0], drdc[1]); |
| 71 | + if(BC(nextP)) continue; |
| 72 | + if(isVisit(visited, nextP)) continue; |
| 73 | + if(getIdAt(nextP) == target){ |
| 74 | + Map[nextP.r][nextP.c] = 0; |
| 75 | + visit(visited, nextP); |
| 76 | + }else if(getIdAt(nextP) == 0){ |
| 77 | + visit(visited, nextP); |
| 78 | + q.offer(nextP); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + // p(Map); |
| 83 | + // System.out.println(); |
| 84 | + } |
| 85 | + |
| 86 | + int a2 = 0; |
| 87 | + for(int r = 0; r<R+2; r++) |
| 88 | + for(int c = 0; c<C+2; c++) |
| 89 | + if(Map[r][c]!=0) a2++; |
| 90 | + return a2; |
| 91 | + } |
| 92 | + |
| 93 | + private void p(int[][] map){ |
| 94 | + for(int[] m: map){ |
| 95 | + System.out.println(Arrays.toString(m)); |
| 96 | + } |
| 97 | + } |
| 98 | + private int getIdAt(Point p){ |
| 99 | + return Map[p.r][p.c]; |
| 100 | + } |
| 101 | + private void visit(boolean[][] v, Point p ){ |
| 102 | + v[p.r][p.c] = true; |
| 103 | + } |
| 104 | + private boolean isVisit(boolean[][] v, Point p ){ |
| 105 | + return v[p.r][p.c]; |
| 106 | + } |
| 107 | + private boolean BC(Point p){ |
| 108 | + int r = p.r; |
| 109 | + int c = p.c; |
| 110 | + return r < 0 || r>= R+2 || c < 0 || c>=C+2; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
0 commit comments