Repository navigation
Expand file tree
/
Copy pathSolution2112.java
More file actions
85 lines (75 loc) · 2.25 KB
/
Copy pathSolution2112.java
File metadata and controls
85 lines (75 loc) · 2.25 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
77
78
79
80
81
82
83
84
85
package swea;
import java.util.*;
import java.io.*;
public class Solution2112 {
static int D, W, K, ans;
static int[][] arr, arr2;
public static boolean check(int[][] arr) {
for (int i = 0; i < W; i++) {
int num = 0;
int cnt = 1;
for (int j = 0; j < D - 1; j++) {
if (arr[j][i] == arr[j + 1][i]) {
cnt++;
} else {
cnt = 1;
}
num = Math.max(num, cnt);
}
if (K > num)
return false;
}
return true;
}
public static void solution(int idx, int cnt) {
if (cnt >= ans)
return;
if (idx == D) {
if (check(arr)) {
ans = Math.min(ans, cnt);
}
return;
}
// 그냥 넘김
solution(idx + 1, cnt);
// A로 변경
for (int i = 0; i < W; i++) {
arr[idx][i] = 0;
}
solution(idx + 1, cnt + 1);
// B로 변경
for (int i = 0; i < W; i++) {
arr[idx][i] = 1;
}
solution(idx + 1, cnt + 1);
for (int i = 0; i < W; i++) {
arr[idx][i] = arr2[idx][i];
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int tc = Integer.parseInt(br.readLine());
for (int i = 1; i <= tc; i++) {
st = new StringTokenizer(br.readLine(), " ");
D = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
arr = new int[D][W];
arr2 = new int[D][W];
for (int j = 0; j < D; j++) {
st = new StringTokenizer(br.readLine());
for (int k = 0; k < W; k++) {
arr[j][k] = arr2[j][k] = Integer.parseInt(st.nextToken());
}
}
if (K == 1 || check(arr)) {
ans = 0;
} else {
ans = K;
solution(0, 0);
}
System.out.println("#" + i + " " + ans);
}
}
}