import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int cases=Integer.parseInt(br.readLine());
for(int i=0;i<cases;i++){
int leds=Integer.parseInt(br.readLine());
int numbers=Integer.parseInt(br.readLine());
int[] arr=new int[numbers];
for(int j=0;j<numbers;j++){
StringBuilder str=new StringBuilder();
StringTokenizer st=new StringTokenizer(br.readLine());
for(int l=0;l<leds;l++){
str.append(st.nextToken());
}
arr[j]=Integer.parseInt(str.toString(),2);
}
int min=Integer.MAX_VALUE;
for(int j=1;j<Math.pow(2, leds);j++){
if(checkUnique(j,leds,arr)){
int counter=countbits(j);
if(min>counter)
min=counter;
}
}
sb.append(min).append("\n");
}
System.out.print(sb);
}
static boolean checkUnique(int mask,int leds,int[]arr){
boolean[]temp=new boolean[(int)Math.pow(2, leds)+1];
for(int i=0;i<arr.length;i++){
int tempNum=mask&arr[i];
if(temp[tempNum]){
return false;
}
temp[tempNum]=true;
}
return true;
}
static int countbits(int j){
String temp=Integer.toBinaryString(j);
int counter=0;
for(int i=0;i<temp.length();i++){
if(temp.charAt(i)=='1'){
counter++;
}
}
return counter;
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int cases=Integer.parseInt(br.readLine());
for(int i=0;i<cases;i++){
int leds=Integer.parseInt(br.readLine());
int numbers=Integer.parseInt(br.readLine());
int[] arr=new int[numbers];
for(int j=0;j<numbers;j++){
StringBuilder str=new StringBuilder();
StringTokenizer st=new StringTokenizer(br.readLine());
for(int l=0;l<leds;l++){
str.append(st.nextToken());
}
arr[j]=Integer.parseInt(str.toString(),2);
}
int min=Integer.MAX_VALUE;
for(int j=1;j<Math.pow(2, leds);j++){
if(checkUnique(j,leds,arr)){
int counter=countbits(j);
if(min>counter)
min=counter;
}
}
sb.append(min).append("\n");
}
System.out.print(sb);
}
static boolean checkUnique(int mask,int leds,int[]arr){
boolean[]temp=new boolean[(int)Math.pow(2, leds)+1];
for(int i=0;i<arr.length;i++){
int tempNum=mask&arr[i];
if(temp[tempNum]){
return false;
}
temp[tempNum]=true;
}
return true;
}
static int countbits(int j){
String temp=Integer.toBinaryString(j);
int counter=0;
for(int i=0;i<temp.length();i++){
if(temp.charAt(i)=='1'){
counter++;
}
}
return counter;
}
}
No comments:
Post a Comment