Skip to content

Commit eafe4e6

Browse files
authored
Merge pull request #224 from AlgorithmWithGod/khj20006
[20250311] BOJ / P5 / 배열에서 이동 / 권혁준
2 parents e3d0841 + 30e9b33 commit eafe4e6

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
7+
class Main {
8+
9+
// IO field
10+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
static StringTokenizer st;
13+
14+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
15+
static int nextInt() {return Integer.parseInt(st.nextToken());}
16+
static long nextLong() {return Long.parseLong(st.nextToken());}
17+
static void bwEnd() throws Exception {bw.flush();bw.close();}
18+
19+
// Additional field
20+
21+
static int N;
22+
static int[][] A;
23+
24+
static int[] dx = {1,0,-1,0};
25+
static int[] dy = {0,1,0,-1};
26+
27+
public static void main(String[] args) throws Exception {
28+
29+
ready();
30+
solve();
31+
32+
bwEnd();
33+
34+
}
35+
36+
static void ready() throws Exception{
37+
38+
N = Integer.parseInt(br.readLine());
39+
A = new int[N][N];
40+
for(int i=0;i<N;i++) {
41+
nextLine();
42+
for(int j=0;j<N;j++) A[i][j] = nextInt();
43+
}
44+
45+
}
46+
47+
static void solve() throws Exception{
48+
49+
int ans = Integer.MAX_VALUE;
50+
while(A[0][0] >= 0) {
51+
ans = Math.min(ans, findMin());
52+
reduce();
53+
}
54+
bw.write(ans + "\n");
55+
56+
}
57+
58+
static void reduce() {
59+
for(int i=0;i<N;i++) for(int j=0;j<N;j++) A[i][j]--;
60+
}
61+
62+
static int findMin() {
63+
64+
int s = 0, e = 200, m = (s+e)>>1;
65+
// m이하인 칸들로만 도달할 수 있는지?
66+
while(s<e) {
67+
if(bfs(m)) e = m;
68+
else s = m+1;
69+
m = (s+e)>>1;
70+
}
71+
return m;
72+
73+
}
74+
75+
// limit 제한 내에서의 bfs
76+
static boolean bfs(int limit) {
77+
78+
if(A[0][0] > limit) return false;
79+
80+
Queue<int[]> Q = new LinkedList<>();
81+
boolean[][] vis = new boolean[N][N];
82+
Q.offer(new int[] {0,0});
83+
vis[0][0] = true;
84+
while(!Q.isEmpty()) {
85+
86+
int[] now = Q.poll();
87+
int x = now[0], y = now[1];
88+
if(x == N-1 && y == N-1) return true;
89+
for(int i=0;i<4;i++) {
90+
int xx = x+dx[i], yy = y+dy[i];
91+
if(safe(xx,yy) && A[xx][yy] <= limit && A[xx][yy] >= 0 && !vis[xx][yy]) {
92+
vis[xx][yy] = true;
93+
Q.offer(new int[] {xx,yy});
94+
}
95+
}
96+
97+
}
98+
99+
return false;
100+
101+
}
102+
103+
static boolean safe(int x, int y) {
104+
return 0<=x&&x<N && 0<=y&&y<N;
105+
}
106+
107+
}
108+
109+
```

0 commit comments

Comments
 (0)