-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECCO 17 R2 P2 - Treasure Hunt.java
146 lines (127 loc) · 3.41 KB
/
ECCO 17 R2 P2 - Treasure Hunt.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.io.*;
import java.util.*;
public class TreasureHunt {
static tile[][] grid;// [row][col]
static int N;
static int keys;
static int tr;
static List<pair> pD;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int T = 0; T < 10; T++) {
N = Integer.parseInt(br.readLine());
String line;
grid = new tile[N][N];
pair s = new pair(-1, -1);
keys = 0;
tr = 0;
pD = new ArrayList<pair>();
for (int i = 0; i < N; i++) {
line = br.readLine();
for (int j = 0; j < N; j++) {
grid[i][j] = new tile(line.charAt(j));
if (line.charAt(j) == 'S') {
s = new pair(i, j);
grid[i][j].vis = true;
}
if (line.charAt(j) == '#')
grid[i][j].vis = true;
}
} // in
bfs(s);
int i = 0;
while (i < pD.size()) {
s = pD.get(i);
if (keys >= grid[s.first][s.second].doorVal) {
bfs(s);
pD.remove(i);
i = 0;
}
else i++;
} // leftover doors
System.out.println(tr);
} // TC
}// MAIN
static void bfs(pair s) {
Queue<pair> todo = new LinkedList<pair>();
todo.add(s);
grid[s.first][s.second].vis = true;
while (!todo.isEmpty()) {
s = todo.poll();
if (grid[s.first][s.second].type == 'T')
tr++;
if (valid(s.first - 1, s.second)) {// up
if (!grid[s.first - 1][s.second].vis) {
if (grid[s.first - 1][s.second].type == 'K')
keys++;
if (grid[s.first - 1][s.second].doorVal <= keys) {
todo.add(new pair(s.first - 1, s.second));
} else {
pD.add( new pair(s.first - 1, s.second));
}
grid[s.first - 1][s.second].vis = true;
}
}
if (valid(s.first + 1, s.second)) {// down
if (!grid[s.first + 1][s.second].vis) {
if (grid[s.first + 1][s.second].type == 'K')
keys++;
if (grid[s.first + 1][s.second].doorVal <= keys) {
todo.add(new pair(s.first + 1, s.second));
} else {
pD.add( new pair(s.first + 1, s.second));
}
grid[s.first + 1][s.second].vis = true;
}
}
if (valid(s.first, s.second - 1)) {// left
if (!grid[s.first][s.second - 1].vis) {
if (grid[s.first][s.second - 1].type == 'K')
keys++;
if (grid[s.first][s.second - 1].doorVal <= keys) {
todo.add(new pair(s.first, s.second - 1));
} else {
pD.add(new pair(s.first, s.second - 1));
}
grid[s.first][s.second - 1].vis = true;
}
}
if (valid(s.first, s.second + 1)) {// right
if (!grid[s.first][s.second + 1].vis) {
if (grid[s.first][s.second + 1].type == 'K')
keys++;
if (grid[s.first][s.second + 1].doorVal <= keys) {
todo.add(new pair(s.first, s.second + 1));
} else {
pD.add( new pair(s.first, s.second + 1));
}
grid[s.first][s.second + 1].vis = true;
}
}
} // bfs while
}
static boolean valid(int r, int c) {
if (r < 0 || c < 0)
return false;
if (r >= N || c >= N)
return false;
return true;
}
static class pair {
int first, second;
pair(int f, int s) {
this.first = f;
this.second = s;
}
}
static class tile {
boolean vis = false;
char type = 'x';
int doorVal = 0;
tile(char c) {
this.type = c;
if (c - 48 > 0 && c - 48 < 10)
this.doorVal = c - 48;
}
}
}// EOF