Given a 2D rectangular matrix of boolean values, write a function which returns whether or not the matrix is the same when rotated 180 degrees. Additionally verify that every boolean true is accessible from every other boolean true if a traversal can be made to an adjacent cell in the matrix, excluding diagonal cells. That is , (x , y ) can access the set [ ( x + 1 , y ) , ( x - 1 , y ) , (x , y - 1 ) , (x , y + 1 ) ] For example, the matrix { { true , false } , { false , true } } should not pass this test.
Utilisateur anonyme
public static boolean isMatrixEqualToFlip(boolean[][] matrix) { if (matrix==null || matrix.length == 0 || matrix[0].length == 0) { return true; } int rowlen = matrix[0].length; int highInd = matrix.length/2; int lowInd = highInd - 1 + (matrix.length % 2); System.out.println("rowlen: " + rowlen + " high: "+ highInd + " low: " + lowInd); while (lowInd >= 0) { System.out.println("high: " + highInd + " lowInd: " + lowInd); for(int i=0; i < rowlen; i++) { System.out.println("Compare " + matrix[highInd][i] + " to " + matrix[lowInd][rowlen - 1 - i]); if (matrix[highInd][i] != matrix[lowInd][rowlen - 1-i]) { return false; } } lowInd--; highInd++; } return true; }