Skip to content

Commit 813e510

Browse files
authored
[20250908] BOJ / G4 / 알고스팟 / 이강현
1 parent eff27fb commit 813e510

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 N,M;
10+
static int[] di = {-1,1,0,0};
11+
static int[] dj = {0,0,-1,1};
12+
static int[][] matrix;
13+
14+
public static void main(String[] args) throws IOException {
15+
st = new StringTokenizer(br.readLine());
16+
M = Integer.parseInt(st.nextToken());
17+
N = Integer.parseInt(st.nextToken());
18+
19+
matrix = new int[N][M];
20+
for (int i = 0; i < N; i++) {
21+
String line = br.readLine();
22+
for (int j = 0; j < M; j++) {
23+
matrix[i][j] = Character.getNumericValue(line.charAt(j));
24+
}
25+
}
26+
bw.write(BFS()+"");
27+
bw.close();
28+
}
29+
public static int BFS(){
30+
ArrayDeque<int[]> deq = new ArrayDeque<>();
31+
boolean[][] visited = new boolean[N][M];
32+
deq.offer(new int[]{0,0,0});
33+
visited[0][0] = true;
34+
35+
while(!deq.isEmpty()){
36+
int[] cur = deq.poll();
37+
38+
if(cur[0] == N-1 && cur[1] == M-1) return cur[2];
39+
40+
for (int i = 0; i < 4; i++) {
41+
int ni = cur[0] + di[i];
42+
int nj = cur[1] + dj[i];
43+
44+
if(ni<0 || ni>=N || nj<0 || nj>=M || visited[ni][nj]) continue;
45+
46+
if(matrix[ni][nj] == 0){
47+
deq.offerFirst(new int[]{ni,nj,cur[2]});
48+
visited[ni][nj] = true;
49+
}else{
50+
deq.offer(new int[]{ni,nj,cur[2]+1});
51+
visited[ni][nj] = true;
52+
}
53+
}
54+
}
55+
return 0;
56+
}
57+
}
58+
```

0 commit comments

Comments
 (0)