Skip to content

Commit 7b34970

Browse files
authored
Merge pull request #466 from AlgorithmWithGod/LiiNi-coder
[20250714] BOJ / G4 / 알고스팟 / 이인희
2 parents e70f7f2 + 292c9e5 commit 7b34970

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayDeque;
6+
import java.util.Arrays;
7+
8+
public class B1261 {
9+
10+
private static BufferedReader br;
11+
private static int m;
12+
private static int n;
13+
private static int[][] map;
14+
private static ArrayDeque<int[]> q;
15+
private static int[][] drdcs = new int[][] {
16+
{0, 1},
17+
{1, 0},
18+
{-1, 0},
19+
{0, -1}
20+
};
21+
private static int[][] visited;
22+
public static void main(String[] args) throws IOException {
23+
br = new BufferedReader(new InputStreamReader(System.in));
24+
String[] temp = br.readLine().split(" ");
25+
m = Integer.parseInt(temp[0]);
26+
n = Integer.parseInt(temp[1]);
27+
map = new int[n][m];
28+
//[i][j]에서 벽을 부순 것의 최소를 담는 배열
29+
visited = new int[n][m];
30+
for(int r = 0; r<n; r++) {
31+
String line = br.readLine();
32+
for(int c = 0; c<m; c++) {
33+
if(line.charAt(c) == '1') {
34+
map[r][c] = 1;
35+
}else
36+
map[r][c] = 0;
37+
visited[r][c] = Integer.MAX_VALUE;
38+
}
39+
}
40+
41+
q = new ArrayDeque<int[]>();
42+
q.add(new int[] {0, 0, 0});
43+
44+
int[] goal = new int[] {n-1, m-1};
45+
int answer = 100*100;
46+
while(!q.isEmpty()) {
47+
int[] temp1 = q.poll();
48+
int[] p = Arrays.copyOfRange(temp1, 0, 2);
49+
int countOfBreakingWall = temp1[2];
50+
// 종료조건
51+
if(isSamePoint(p, goal)) {
52+
if(countOfBreakingWall==0) {
53+
answer = 0;
54+
break;
55+
}
56+
else {
57+
answer = Math.min(answer, countOfBreakingWall);
58+
continue;
59+
}
60+
}
61+
for(int[] drdc : drdcs ) {
62+
int[] nextPoint = new int[]{p[0] + drdc[0], p[1]+ drdc[1]};
63+
if(nextPoint[0] < 0 || nextPoint[0] >= n || nextPoint[1] < 0 || nextPoint[1] >= m)
64+
continue;
65+
int nextCountOfBreakingWall = countOfBreakingWall;
66+
//벽이면
67+
if(getValueAtPoint(map, nextPoint) == 1) {
68+
if(getValueAtPoint(visited, nextPoint) > nextCountOfBreakingWall) {
69+
nextCountOfBreakingWall++;
70+
visited[p[0]][p[1]] = countOfBreakingWall;
71+
q.addLast(new int[] {nextPoint[0], nextPoint[1], nextCountOfBreakingWall});
72+
}
73+
}else {
74+
if(getValueAtPoint(visited, nextPoint) > nextCountOfBreakingWall) {
75+
visited[p[0]][p[1]] = countOfBreakingWall;
76+
q.addFirst(new int[] {nextPoint[0], nextPoint[1], nextCountOfBreakingWall});
77+
}
78+
}
79+
}
80+
}
81+
82+
System.out.println(answer);
83+
}
84+
85+
private static int getValueAtPoint(int[][] matrix, int[] p) {
86+
return matrix[p[0]][p[1]];
87+
}
88+
89+
private static boolean isSamePoint(int[] p, int[] q) {
90+
return p[0] == q[0] && p[1] == q[1];
91+
}
92+
93+
}
94+
```

0 commit comments

Comments
 (0)