Repository navigation
Expand file tree
/
Copy pathSolution7208.java
More file actions
64 lines (59 loc) · 1.62 KB
/
Copy pathSolution7208.java
File metadata and controls
64 lines (59 loc) · 1.62 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
package swea;
import java.util.Scanner;
public class Solution7208 {
static int t, n, ans;
static int[][] arr;
static int[] color = new int[4];
static int[] numbers;
static boolean[] selected;
public static boolean check(){
for (int i = 0; i < n-1; i++) {
for (int j = 1+i; j < n; j++) {
if(arr[i][j]==1){
if(numbers[i]==numbers[j]) {
return false;
}
}
}
}
return true;
}
public static void permutation(int cnt){
if(cnt==n){
if(check()){
int num=0;
for (int i = 0; i < n; i++) {
if(color[i]!=numbers[i]) num++;
}
ans=Math.min(ans,num);
}
return;
}
for(int i=0;i<4;i++){
numbers[cnt]=i;
permutation(cnt+1);
}
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
t=sc.nextInt();
for (int i = 0; i < t; i++) {
n=sc.nextInt();
arr=new int[n][n];
numbers=new int[n];
selected=new boolean[n];
color=new int[n];
ans=8;
for (int j = 0; j < n; j++) {
color[j]=sc.nextInt()-1;
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
arr[j][k]=sc.nextInt();
}
}
permutation(0);
System.out.printf("#%d %d%n",i+1,ans);
}
}
}