Skip to content

Commit b38fbfe

Browse files
authored
Merge pull request #381 from AlgorithmWithGod/lkhyun
[20250623] BOJ / G1 / 열쇠 / 이강현
2 parents 9cd02b6 + 5a8e8c6 commit b38fbfe

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

lkhyun/202506/23 BOJ G1 열쇠.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int[] dx = {-1,1,0,0};
10+
static int[] dy = {0,0,-1,1};
11+
12+
public static void main(String[] args) throws IOException {
13+
int T = Integer.parseInt(br.readLine());
14+
for (int t = 0; t < T; t++) {
15+
st = new StringTokenizer(br.readLine());
16+
int h = Integer.parseInt(st.nextToken());
17+
int w = Integer.parseInt(st.nextToken());
18+
char[][] map = new char[h][w];
19+
20+
for (int i = 0; i < h; i++) {
21+
String line = br.readLine();
22+
for (int j = 0; j < w; j++) {
23+
map[i][j] = line.charAt(j);
24+
}
25+
}
26+
27+
String keyInput = br.readLine();
28+
Set<Character> keys = new HashSet<>();
29+
if (!keyInput.equals("0")) {
30+
for (char c : keyInput.toCharArray()) {
31+
keys.add(c);
32+
}
33+
}
34+
35+
int documents = 0;
36+
while (true) {
37+
Set<Character> prevKeys = new HashSet<>(keys);
38+
39+
int[] result = BFS(map, h, w, keys);
40+
documents = result[0];
41+
42+
boolean foundNewKey = false;
43+
for (char key : keys) {
44+
if (!prevKeys.contains(key)) {
45+
foundNewKey = true;
46+
break;
47+
}
48+
}
49+
50+
if (!foundNewKey) {
51+
break;
52+
}
53+
}
54+
bw.write(documents + "\n");
55+
}
56+
bw.close();
57+
}
58+
59+
static int[] BFS(char[][] map, int h, int w, Set<Character> keys) {
60+
ArrayDeque<int[]> q = new ArrayDeque<>();
61+
boolean[][] visited = new boolean[h][w];
62+
int documents = 0;
63+
64+
for (int i = 0; i < h; i++) {
65+
for (int j = 0; j < w; j++) {
66+
if ((i == 0 || i == h-1 || j == 0 || j == w-1) && map[i][j] != '*') {
67+
if (canMove(map[i][j], keys)) {
68+
visited[i][j] = true;
69+
q.offer(new int[]{i, j});
70+
71+
if (map[i][j] == '$') {
72+
documents++;
73+
} else if (isLowerCase(map[i][j])) {
74+
keys.add(map[i][j]);
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
while (!q.isEmpty()) {
82+
int[] curr = q.poll();
83+
int x = curr[0], y = curr[1];
84+
85+
for (int d = 0; d < 4; d++) {
86+
int nx = x + dx[d];
87+
int ny = y + dy[d];
88+
89+
if (nx < 0 || nx >= h || ny < 0 || ny >= w || visited[nx][ny]) {
90+
continue;
91+
}
92+
93+
char cell = map[nx][ny];
94+
95+
if (canMove(cell, keys)) {
96+
visited[nx][ny] = true;
97+
q.offer(new int[]{nx, ny});
98+
99+
if (cell == '$') {
100+
documents++;
101+
} else if (isLowerCase(cell)) {
102+
keys.add(cell);
103+
}
104+
}
105+
}
106+
}
107+
108+
return new int[]{documents};
109+
}
110+
111+
static boolean canMove(char cell, Set<Character> keys) {
112+
if (cell == '*') return false;
113+
if (cell == '.' || cell == '$') return true;
114+
if (isLowerCase(cell)) return true;
115+
if (isUpperCase(cell)) {
116+
return keys.contains(Character.toLowerCase(cell));
117+
}
118+
return false;
119+
}
120+
121+
static boolean isUpperCase(char c) {
122+
return c >= 'A' && c <= 'Z';
123+
}
124+
125+
static boolean isLowerCase(char c) {
126+
return c >= 'a' && c <= 'z';
127+
}
128+
}
129+
```

0 commit comments

Comments
 (0)