-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain2.java
96 lines (80 loc) Β· 2.67 KB
/
Main2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package BFS.P16236;
import java.io.*;
import java.util.*;
public class Main2 {
static int N, ans;
static int[][] map;
static Pos from, to;
static int[] di = {-1, 0, 0, 1};
static int[] dj = {0, -1, 1, 0};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BFS/P16236/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 9) {
from = new Pos(i, j, 0);
map[i][j] = 0;
}
}
}
int size = 2, eat = 0;
while ( (to = bfs(size)) != null ) {
if (size == ++eat) { size ++; eat = 0; }
ans += to.time;
from = new Pos(to.i, to.j, 0);
}
System.out.println(ans);
}
static Pos bfs(int size) {
Queue<Pos> q = new LinkedList<>();
boolean[][] visited = new boolean[N][N];
q.offer(new Pos(from.i, from.j, 0));
visited[from.i][from.j] = true;
Pos min = null;
while (!q.isEmpty()) {
Pos p = q.poll();
for (int k = 0; k < 4; k++) {
int to_i = p.i + di[k];
int to_j = p.j + dj[k];
if (isValidPath(to_i, to_j) && !visited[to_i][to_j] && map[to_i][to_j] <= size) {
visited[to_i][to_j] = true;
Pos to = new Pos(to_i, to_j, p.time+1);
if (0 < map[to_i][to_j] && map[to_i][to_j] < size) {
if (min == null) min = to;
else min = compare(min, to);
} else q.offer(to);
}
}
}
if (min != null) map[min.i][min.j] = 0;
return min;
}
static Pos compare(Pos a, Pos b) {
if (a.time < b.time) return a;
else if (a.time > b.time) return b;
else {
if (a.i < b.i) return a;
else if (a.i > b.i) return b;
else {
if (a.j < b.j) return a;
else return b;
}
}
}
static boolean isValidPath(int i, int j) {
return 0 <= i && i < N && 0 <= j && j < N;
}
static class Pos {
int i, j, time;
public Pos(int i, int j, int time) {
this.i = i;
this.j = j;
this.time = time;
}
}
}