-
Notifications
You must be signed in to change notification settings - Fork 22
/
bcake.java
51 lines (43 loc) · 1.37 KB
/
bcake.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
import java.io.*;
import java.util.*;
public class bcake {
private static BufferedReader in;
private static PrintWriter out;
public static void main (String [] args) throws IOException {
in = new BufferedReader (new InputStreamReader (System.in));
out = new PrintWriter (System.out, true);
int T = Integer.parseInt (in.readLine());
StringTokenizer st;
while (T-- > 0) {
st = new StringTokenizer (in.readLine ());
int H = Integer.parseInt (st.nextToken ());
int W = Integer.parseInt (st.nextToken ());
int K = Integer.parseInt (st.nextToken ());
if (K == 0) {
out.println (0);
continue;
}
int [][] grid = new int [H + 1][W + 1];
for (int i = 1; i <= H; i++) {
char [] line = in.readLine().toCharArray();
for (int j = 1; j <= W; j++) {
grid [i][j] = grid [i - 1][j] + grid [i][j - 1]
+ (line [j - 1] == 'C' ? 1 : 0) - grid [i - 1][j - 1];
}
}
int min = Integer.MAX_VALUE;
for (int i = 1; i <= H; i++)
for (int j = i + 1; j <= H; j++)
for (int k = 1; k <= W; k++)
for (int m = k + 1; m <= W; m++) {
int chips = grid [j][m] - grid [i][m] - grid [j][k] + grid [i][k];
if (chips != K) continue;
if ((m - k) * (j - i) < min)
min = (m - k) * (j - i);
}
out.println (min == Integer.MAX_VALUE ? -1 : min);
}
out.close();
System.exit(0);
}
}