ToggleColumnSol

 avatar
ptrdung
plain_text
2 months ago
1.8 kB
0
Indexable
Never
package training1;

import java.util.Scanner;

public class ToggleColum {
	static int countZ(int[][] data, int row) {
		int count = 0;
		for(int i = 0; i < data[0].length; i++) {
			if(data[row][i] == 0) {
				count++;
			}
		}
		return count;
	}
	static void flip(int[][] data, int col) {
		for(int i = 0; i < data.length; i++) {
			data[i][col] = (data[i][col] == 0)?1:0;
		}
	}
	static int countOne(int[][] data) {
		int count = 0;
		for(int i = 0; i < data.length; i++) {
			int flag = 0;
			for(int j = 0; j < data[0].length; j++) {
				if(data[i][j] == 0) {
					flag = 1;
					break;
				}
			}
			if(flag == 0) {
				count++;
			}
		}
		return count;
	}
	static void solution(int[] resuilt, int[][] data, int K) {
		for(int i = 0; i < data.length; i++) {
			int countZ = countZ(data, i);
			int[] visited = new int[data[0].length];
			if(countZ > K || (K-countZ) % 2 == 1) {
				continue;
			}
			for(int k = 0; k < data[0].length; k++) {
				if(data[i][k] == 0) {
					visited[k] = 1;
					flip(data, k);
				}
			}
			int numOne = countOne(data);
			if(numOne > resuilt[0]) {
				resuilt[0] = numOne;
			}
			for(int k = 0; k < data[0].length; k++) {
				if(visited[k] == 1) {
					flip(data, k);
				}
			}
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int t = 1; t <= T; t++) {
			int N = sc.nextInt();
			int M = sc.nextInt();
			int K = sc.nextInt();
			int[][] data = new int[N][M];
			for(int i = 0; i < N; i++) {
				for(int j = 0; j < M; j++) {
					data[i][j] = sc.nextInt();
				}
			}
			int[] resuilt = {0};
			solution(resuilt, data, K);
			System.out.println(resuilt[0]);
		}
	}
}
Leave a Comment