|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static int h, w; |
| 8 | + static char[][] map; |
| 9 | + static boolean[][] visited; |
| 10 | + static boolean[] key; |
| 11 | + static List<int[]>[] door; |
| 12 | + static final int[] dr = {-1, 1, 0, 0}; |
| 13 | + static final int[] dc = {0, 0, -1, 1}; |
| 14 | + |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 17 | + StringBuilder out = new StringBuilder(); |
| 18 | + |
| 19 | + int T = Integer.parseInt(br.readLine().trim()); |
| 20 | + for(int t=0;t<T;t++){ |
| 21 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 22 | + h = Integer.parseInt(st.nextToken()); |
| 23 | + w = Integer.parseInt(st.nextToken()); |
| 24 | + |
| 25 | + map = new char[h + 2][w + 2]; |
| 26 | + for (int i = 0; i < h + 2; i++) Arrays.fill(map[i], '.'); |
| 27 | + |
| 28 | + for (int r = 1; r <= h; r++) { |
| 29 | + String line = br.readLine(); |
| 30 | + for (int c = 1; c <= w; c++) { |
| 31 | + map[r][c] = line.charAt(c - 1); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + key = new boolean[26]; |
| 36 | + door = new ArrayList[26]; |
| 37 | + for (int i = 0; i < 26; i++) door[i] = new ArrayList<>(); |
| 38 | + |
| 39 | + String keys = br.readLine().trim(); |
| 40 | + if (!keys.equals("0")) { |
| 41 | + for (int i = 0; i < keys.length(); i++) { |
| 42 | + char ch = keys.charAt(i); |
| 43 | + if ('a' <= ch && ch <= 'z') key[ch - 'a'] = true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + out.append(bfs()).append('\n'); |
| 48 | + } |
| 49 | + |
| 50 | + System.out.print(out.toString()); |
| 51 | + } |
| 52 | + |
| 53 | + static int bfs() { |
| 54 | + visited = new boolean[h + 2][w + 2]; |
| 55 | + Deque<int[]> q = new ArrayDeque<>(); |
| 56 | + q.offer(new int[]{0, 0}); |
| 57 | + visited[0][0] = true; |
| 58 | + |
| 59 | + int docs = 0; |
| 60 | + |
| 61 | + while (!q.isEmpty()) { |
| 62 | + int[] cur = q.poll(); |
| 63 | + int r = cur[0], c = cur[1]; |
| 64 | + |
| 65 | + for (int d = 0; d < 4; d++) { |
| 66 | + int nr = r + dr[d]; |
| 67 | + int nc = c + dc[d]; |
| 68 | + |
| 69 | + if (nr < 0 || nr >= h + 2 || nc < 0 || nc >= w + 2) continue; |
| 70 | + if (visited[nr][nc]) continue; |
| 71 | + |
| 72 | + char ch = map[nr][nc]; |
| 73 | + |
| 74 | + if (ch == '*') continue; |
| 75 | + |
| 76 | + if ('A' <= ch && ch <= 'Z') { |
| 77 | + int idx = ch - 'A'; |
| 78 | + if (!key[idx]) { |
| 79 | + door[idx].add(new int[]{nr, nc}); |
| 80 | + continue; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if ('a' <= ch && ch <= 'z') { |
| 85 | + int idx = ch - 'a'; |
| 86 | + if (!key[idx]) { |
| 87 | + key[idx] = true; |
| 88 | + for (int[] pos : door[idx]) { |
| 89 | + int rr = pos[0], cc = pos[1]; |
| 90 | + if (!visited[rr][cc]) { |
| 91 | + visited[rr][cc] = true; |
| 92 | + q.offer(new int[]{rr, cc}); |
| 93 | + } |
| 94 | + } |
| 95 | + door[idx].clear(); |
| 96 | + } |
| 97 | + map[nr][nc] = '.'; |
| 98 | + } |
| 99 | + |
| 100 | + if (ch == '$') { |
| 101 | + docs++; |
| 102 | + map[nr][nc] = '.'; |
| 103 | + } |
| 104 | + |
| 105 | + visited[nr][nc] = true; |
| 106 | + q.offer(new int[]{nr, nc}); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return docs; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +``` |
0 commit comments