Skip to content

Commit 12c6338

Browse files
authored
Merge pull request #303 from AlgorithmWithGod/khj20006
[20250408] BOJ / G1 / 달이 차오른다, 가자. / 권혁준
2 parents a0650cb + d32fc92 commit 12c6338

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
while(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static int N, M;
26+
static char[][] A;
27+
static int[] start;
28+
29+
static int[] dx = {1,0,-1,0};
30+
static int[] dy = {0,1,0,-1};
31+
32+
public static void main(String[] args) throws Exception {
33+
34+
ready();
35+
solve();
36+
37+
bwEnd();
38+
39+
}
40+
41+
static void ready() throws Exception{
42+
43+
N = nextInt();
44+
M = nextInt();
45+
A = new char[N][M];
46+
for(int i=0;i<N;i++) {
47+
A[i] = br.readLine().toCharArray();
48+
for(int j=0;j<M;j++) if(A[i][j] == '0') start = new int[] {i,j};
49+
}
50+
51+
}
52+
53+
static void solve() throws Exception{
54+
55+
boolean[][][] vis = new boolean[N][M][64];
56+
Queue<int[]> Q = new LinkedList<>();
57+
vis[start[0]][start[1]][0] = true;
58+
Q.offer(new int[] {start[0], start[1], 0, 0});
59+
while(!Q.isEmpty()) {
60+
int[] now = Q.poll();
61+
int x = now[0], y = now[1], keys = now[2], t = now[3];
62+
if(A[x][y] == '1') {
63+
bw.write(t + "\n");
64+
return;
65+
}
66+
for(int i=0;i<4;i++) {
67+
int xx = x+dx[i], yy = y+dy[i];
68+
if(xx<0 || xx>=N || yy<0 || yy>=M || A[xx][yy] == '#') continue;
69+
int nkeys = keys;
70+
if(A[xx][yy] >= 'a') nkeys |= (1<<(A[xx][yy]-'a'));
71+
if('A' <= A[xx][yy] && A[xx][yy] <= 'F' && (nkeys & (1<<(A[xx][yy]-'A'))) == 0) continue;
72+
if(vis[xx][yy][nkeys]) continue;
73+
vis[xx][yy][nkeys] = true;
74+
Q.offer(new int[] {xx,yy,nkeys,t+1});
75+
}
76+
}
77+
bw.write("-1");
78+
79+
}
80+
81+
}
82+
83+
```

0 commit comments

Comments
 (0)