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 |
private static Int32 NumOfSubRectanges(Int32 m, Int32 n) {
return m * n * (m + 1) * (n + 1) / 4 - 1;
}
public static void Generate(String input, String output) {
using (FileStream fs = new FileStream(input, FileMode.Open, FileAccess.Read, FileShare.None), ws = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (StreamReader reader = new StreamReader(fs)) {
using (StreamWriter writer = new StreamWriter(ws)) {
String line = String.Empty;
line = reader.ReadLine();
if (line == null) return;
Int32 start_idx = 0, end_idx = 0, prev = 0;
Int32 sumOfSubRects=0;
Int32 numOfRects=0;
ArrayList items = new ArrayList();
while ((line=reader.ReadLine()) != null) {
items.Add(line);
}
for (Int32 i = 0; i < items.Count; ++i) {
String current = items[i] as String;
while ((start_idx = current.IndexOf('1',prev)) != -1) {
end_idx = current.IndexOf('0', (start_idx == current.Length -1) ? start_idx : start_idx + 1);
end_idx = (end_idx == -1) ? current.Length : end_idx;
prev = end_idx + 1;
--end_idx;
if (i != 0 && ((String)items[i - 1])[start_idx] == '1' && ((String)items[i - 1])[end_idx] == '1') {
if (start_idx > 0 && end_idx < current.Length - 1) {
if (((String)items[i - 1])[start_idx - 1] == '0' && ((String)items[i - 1])[end_idx + 1] == '0') continue;
} else if (start_idx > 0) {
if (((String)items[i - 1])[start_idx - 1] == '0') continue;
} else if (end_idx < current.Length -1 ) {
if (((String)items[i - 1])[end_idx + 1] == '0') continue;
}
}
Int32 n = end_idx - start_idx + 1;
Int32 m = 1;
Int32 j=i;
while (++j < items.Count) {
if (((String)items[j])[start_idx] == '0' || ((String)items[j])[end_idx] == '0') break;
if (start_idx > 0 && end_idx < current.Length - 1) {
if (((String)items[j])[start_idx - 1] != '0' || ((String)items[j])[end_idx + 1] != '0') break;
} else if (start_idx > 0) {
if (((String)items[j])[start_idx - 1] != '0') break;
} else if (end_idx < current.Length -1 ) {
if (((String)items[j])[end_idx + 1] != '0') break;
}
++m;
}
sumOfSubRects += NumOfSubRectanges(m,n);
++numOfRects;
if (prev >=current.Length) break;
}
prev=0;
}
writer.WriteLine("A: {0}\tB: {1}", numOfRects, sumOfSubRects);
}
}
}
} |