-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcastle.java
More file actions
76 lines (70 loc) · 2.32 KB
/
castle.java
File metadata and controls
76 lines (70 loc) · 2.32 KB
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
import java.io.*;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class castle {
static int[] union;
static ArrayList<Integer>[] adj;
static int[] roomsizes;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("castle.in"));
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("castle.out")));
//Scanner reader = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(reader.readLine());
int m = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
adj = new ArrayList[m*n];
union = new int[m*n];
roomsizes = new int[m*n];
int room;
for(int i = 0; i < n; i++) {
st = new StringTokenizer(reader.readLine());
for (int j = 0; j < m; j++) {
adj[i * m + j] = new ArrayList<Integer>();
room = Integer.parseInt(st.nextToken());
if (room >= 8) {
room -= 8;
adj[i * m + j].add((i + 1) * m + j);
}
if (room >= 4) {
room -= 4;
adj[i * m + j].add(i * m + j + 1);
}
if (room >= 2) {
room -= 2;
adj[i * m + j].add((i - 1) * m + j);
}
if (room >= 1) {
room--;
adj[i * m + j].add(i * m + j - 1);
}
}
}
int group = 1;
for(int i = 0; i < m*n; i++) {
if (union[i] < 1) {
union[i] = group;
roomsizes[group]++;
flood(i);
group++;
}
}
int maxroom1 = 1, maxroom2 = 2;
for(int i = 1; i <= group; i++)
{
}
reader.close();
writer.close();
}
public static void flood(int i)
{
for(int j : adj[i])
{
if(union[j] < 1) {
union[j] = union[i];
roomsizes[union[j]]++;
flood(j);
}
}
}
}