Skip to content

Commit 2538f8d

Browse files
authored
[20250829] BOJ / P5 / 바리스타의 힘 / 권혁준
1 parent bb31d0e commit 2538f8d

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static final int INF = (int)1e9 + 7;
57+
static final int[] dx = {1,0,-1,0};
58+
static final int[] dy = {0,1,0,-1};
59+
60+
static int N, M;
61+
static boolean[][] wall;
62+
63+
public static void main(String[] args) throws Exception {
64+
65+
io = new IOController();
66+
67+
N = io.nextInt();
68+
M = io.nextInt();
69+
wall = new boolean[N][M];
70+
for(int i=0;i<N;i++) {
71+
char[] tmp = io.nextToken().toCharArray();
72+
for(int j=0;j<M;j++) wall[i][j] = tmp[j] == '1';
73+
}
74+
75+
int[][] from = bfs(0,0, false);
76+
int[][] to = bfs(N-1,M-1, true);
77+
78+
int[][] right = new int[N][M];
79+
for(int i=0;i<N;i++) {
80+
right[i][M-1] = Math.min(INF, to[i][M-1] + M-1);
81+
for(int j=M-2;j>=0;j--) {
82+
right[i][j] = Math.min(right[i][j+1], to[i][j] + j);
83+
}
84+
}
85+
86+
int[][] up = new int[N][M];
87+
for(int j=0;j<M;j++) {
88+
up[N-1][j] = Math.min(INF, to[N-1][j] + N-1);
89+
for(int i=N-2;i>=0;i--) {
90+
up[i][j] = Math.min(up[i+1][j], to[i][j] + i);
91+
}
92+
}
93+
94+
int ans = INF;
95+
for(int i=0;i<N;i++) for(int j=0;j<M;j++) if(from[i][j] != INF) {
96+
int res = Math.min(from[i][j] + right[i][j] - j, from[i][j] + up[i][j] - i);
97+
ans = Math.min(ans, res);
98+
}
99+
io.write((ans == INF ? "-1" : ans) + "\n");
100+
101+
io.close();
102+
103+
}
104+
105+
static int[][] bfs(int a, int b, boolean can) {
106+
int[][] res = new int[N][M];
107+
for(int i=0;i<N;i++) Arrays.fill(res[i], INF);
108+
109+
boolean[][] vis = new boolean[N][M];
110+
Queue<int[]> q = new ArrayDeque<>();
111+
q.offer(new int[]{a,b,0});
112+
vis[a][b] = true;
113+
while(!q.isEmpty()) {
114+
int[] cur = q.poll();
115+
int x = cur[0], y = cur[1], t = cur[2];
116+
res[x][y] = t;
117+
for(int i=0;i<4;i++) {
118+
int xx = x+dx[i], yy = y+dy[i];
119+
if(xx<0 || xx>=N || yy<0 || yy>=M || vis[xx][yy]) continue;
120+
if(wall[xx][yy]) {
121+
if(can) {
122+
res[xx][yy] = t+1;
123+
vis[xx][yy] = true;
124+
}
125+
continue;
126+
}
127+
q.offer(new int[]{xx,yy,t+1});
128+
vis[xx][yy] = true;
129+
}
130+
}
131+
return res;
132+
}
133+
134+
}
135+
```

0 commit comments

Comments
 (0)