Last active
December 21, 2015 17:09
-
-
Save alcedo/6338542 to your computer and use it in GitHub Desktop.
Problem_541.java error collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Arrays; | |
| import java.util.Scanner; | |
| import static java.lang.System.out; | |
| /** | |
| * Idea: if any col or row is odd, then set row or column to be corrupted | |
| * | |
| * if more than 1 row or col is corrupted, then its deadEnd. | |
| * else | |
| * output the particular row or col to be flipped in order to fix it | |
| * | |
| * 20 mins coding | |
| * 15 mins to reading questions | |
| */ | |
| public class Main { | |
| public static void main(String[] args) { | |
| Scanner in = new Scanner(System.in); | |
| int size = 0; | |
| while( (size=in.nextInt()) != 0) { | |
| //read matrix | |
| int[][] matrix = new int[size][size]; | |
| for(int i=0; i<size; i++) | |
| for(int j=0; j<size; j++) { | |
| matrix[i][j] = in.nextInt(); | |
| } | |
| int rowCorrupted = -1; | |
| int colCorrupted = -1; | |
| boolean deadEnd = false; | |
| // sum cols | |
| int[] sumCol = new int[size]; | |
| int[] sumRow = new int[size]; | |
| for(int i=0; i<size; i++) { | |
| for(int j=0; j<size; j++) { | |
| sumRow[i] += matrix[i][j]; | |
| sumCol[i] += matrix[j][i]; | |
| } | |
| //row corrupted | |
| if(sumRow[i] % 2 != 0) { | |
| if(rowCorrupted > 0) //set previously, more than 1 row corrupted | |
| deadEnd = true; | |
| rowCorrupted = i; | |
| } | |
| //col corrupted | |
| if(sumCol[i] % 2 != 0) { | |
| if(colCorrupted > 0) //set previously, more than 1 cpl corrupted | |
| deadEnd = true; | |
| colCorrupted = i; | |
| } | |
| } | |
| if(colCorrupted < 0 && rowCorrupted < 0) { | |
| out.println("OK"); | |
| }else { | |
| if(deadEnd) | |
| out.println("Corrupt"); | |
| else | |
| out.println("Change bit (" + (rowCorrupted+1) + "," + (colCorrupted+1) + ")"); | |
| } | |
| //printMatrix(matrix); | |
| //out.println(size); | |
| } | |
| }//end of void main | |
| public static void printMatrix(int[][] matrix) { | |
| int cols = matrix[0].length; | |
| int rows = matrix.length; | |
| for(int i=0; i<rows; i++){ | |
| for(int j=0; j<cols; j++) { | |
| System.out.print(matrix[i][j] + " "); | |
| } | |
| System.out.println(""); | |
| } | |
| } | |
| }//end of class main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment