-
Notifications
You must be signed in to change notification settings - Fork 0
/
castle.java
116 lines (105 loc) · 2.72 KB
/
castle.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
/*
ID: 001207j1
LANG: JAVA
TASK: castle
*/
import java.io.*;
import java.util.*;
public class castle {
public static int comp_num = 1;
public static int[] comp;
public static Map<Integer, Integer> map = new HashMap<Integer, Integer>();
public static void main(String[] args) throws IOException {
// long start_Time = System.currentTimeMillis();
BufferedReader f = new BufferedReader(new FileReader("castle.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("castle.out")));
String[] t = f.readLine().split(" ");
int c = Integer.parseInt(t[0]);
int r = Integer.parseInt(t[1]);
int[][] cas = new int[r * c][4];
comp = new int[r * c];
Arrays.fill(comp, -2);
for (int i = 0; i < r; i++) {
t = f.readLine().split(" ");
for (int j = 0; j < c; j++) {
String s = Integer.toBinaryString(Integer.parseInt(t[j]));
for (int k = 0; k < s.length(); k++) {
cas[i * c + j][k] = Integer.parseInt(s.charAt(s.length() - k - 1) + "");
// 1 w;2 n;4 e;8 s;
}
}
}
flood_fill(cas, r, c);
/*
* for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) {
* System.out.print(comp[i * c + j] + " "); } System.out.println(); }
*/
out.println(comp_num);
int max = 0;
for (int i = 1; i <= comp_num; i++)
if (map.get(i) > max)
max = map.get(i);
out.println(max);
int wl = 0;
char d = 'N';
for (int j = 0; j < c; j++)
for (int i = r - 1; i >= 0; i--) {
if (i > 0 && comp[i * c + j] != comp[(i - 1) * c + j]) {
int n = map.get(comp[i * c + j]) + map.get(comp[(i - 1) * c + j]);
if (n > max) {
max = n;
wl = i * c + j;
d = 'N';
}
}
if (j < c - 1 && comp[i * c + j] != comp[i * c + j + 1]) {
int n = map.get(comp[i * c + j]) + map.get(comp[i * c + j + 1]);
if (n > max) {
max = n;
wl = i * c + j;
d = 'E';
}
}
}
out.println(max);
int r_max = (int)wl/c + 1;
int c_max = wl- (r_max - 1) * c + 1;
out.println(r_max + " " + c_max + " " + d);
f.close();
out.close();
}
public static int dfs(int[][] cas, int s, int c, int cnt) {
comp[s] = comp_num;
cnt++;
for (int k = 0; k < 4; k++)
if (cas[s][k] == 0) {
int w = dir(s, k, c);
if (comp[w] != -2)
continue;
comp[w] = comp_num;
cnt = dfs(cas, w, c, cnt);
}
return cnt;
}
public static void flood_fill(int[][] cas, int r, int c) {
for (int i = 0; i < r * c; i++) {
int cnt = 0;
if (comp[i] != -2)
continue;
cnt = dfs(cas, i, c, cnt);
map.put(comp_num, cnt);
comp_num++;
}
comp_num--;
}
public static int dir(int idx, int k, int c) {
if (k == 0)
return idx - 1;
else if (k == 1)
return idx - c;
else if (k == 2)
return idx + 1;
else
return idx + c;
}
}