import java.io.*; import java.util.*; public class Solution { // Test to see if its solved - All rows sum to 15 along with diagonals public static boolean solved(int[][] test) { for(int i = 0; i < 3; i++) { if((test[i][0] + test[i][1] + test[i][2])!= 15) return false; if((test[0][i] + test[1][i] + test[2][i])!= 15) return false; } if((test[0][0] + test[1][1] + test[2][2]) != 15) return false; if((test[0][2] + test[1][1] + test[2][0]) != 15) return false; return true; } // Hard coded the solutions // Return minimum score of solution public static int minScore(int[][] square) { int score = 1000; int tempScore; // case 1 tempScore = 0; tempScore = tempScore + Math.abs(square[0][0]-8); tempScore = tempScore + Math.abs(square[0][1]-1); tempScore = tempScore + Math.abs(square[0][2]-6); tempScore = tempScore + Math.abs(square[1][0]-3); tempScore = tempScore + Math.abs(square[1][2]-7); tempScore = tempScore + Math.abs(square[2][0]-4); tempScore = tempScore + Math.abs(square[2][1]-9); tempScore = tempScore + Math.abs(square[2][2]-2); if(tempScore <= score) { score = tempScore; } // case 2 tempScore = 0; tempScore = tempScore + Math.abs(square[0][0]-4); tempScore = tempScore + Math.abs(square[0][1]-3); tempScore = tempScore + Math.abs(square[0][2]-8); tempScore = tempScore + Math.abs(square[1][0]-9); tempScore = tempScore + Math.abs(square[1][2]-1); tempScore = tempScore + Math.abs(square[2][0]-2); tempScore = tempScore + Math.abs(square[2][1]-7); tempScore = tempScore + Math.abs(square[2][2]-6); if(tempScore <= score) { score = tempScore; } // case 3 tempScore = 0; tempScore = tempScore + Math.abs(square[0][0]-2); tempScore = tempScore + Math.abs(square[0][1]-9); tempScore = tempScore + Math.abs(square[0][2]-4); tempScore = tempScore + Math.abs(square[1][0]-7); tempScore = tempScore + Math.abs(square[1][2]-3); tempScore = tempScore + Math.abs(square[2][0]-6); tempScore = tempScore + Math.abs(square[2][1]-1); tempScore = tempScore + Math.abs(square[2][2]-8); if(tempScore <= score) { score = tempScore; } // case 4 tempScore = 0; tempScore = tempScore + Math.abs(square[0][0]-6); tempScore = tempScore + Math.abs(square[0][1]-7); tempScore = tempScore + Math.abs(square[0][2]-2); tempScore = tempScore + Math.abs(square[1][0]-1); tempScore = tempScore + Math.abs(square[1][2]-9); tempScore = tempScore + Math.abs(square[2][0]-8); tempScore = tempScore + Math.abs(square[2][1]-3); tempScore = tempScore + Math.abs(square[2][2]-4); if(tempScore <= score) { score = tempScore; } // case 5 tempScore = 0; tempScore = tempScore + Math.abs(square[0][0]-6); tempScore = tempScore + Math.abs(square[0][1]-1); tempScore = tempScore + Math.abs(square[0][2]-8); tempScore = tempScore + Math.abs(square[1][0]-7); tempScore = tempScore + Math.abs(square[1][2]-3); tempScore = tempScore + Math.abs(square[2][0]-2); tempScore = tempScore + Math.abs(square[2][1]-9); tempScore = tempScore + Math.abs(square[2][2]-4); if(tempScore <= score) { score = tempScore; } // case 6 tempScore = 0; tempScore = tempScore + Math.abs(square[0][0]-8); tempScore = tempScore + Math.abs(square[0][1]-3); tempScore = tempScore + Math.abs(square[0][2]-4); tempScore = tempScore + Math.abs(square[1][0]-1); tempScore = tempScore + Math.abs(square[1][2]-9); tempScore = tempScore + Math.abs(square[2][0]-6); tempScore = tempScore + Math.abs(square[2][1]-7); tempScore = tempScore + Math.abs(square[2][2]-2); if(tempScore <= score) { score = tempScore; } // case 7 tempScore = 0; tempScore = tempScore + Math.abs(square[0][0]-4); tempScore = tempScore + Math.abs(square[0][1]-9); tempScore = tempScore + Math.abs(square[0][2]-2); tempScore = tempScore + Math.abs(square[1][0]-3); tempScore = tempScore + Math.abs(square[1][2]-7); tempScore = tempScore + Math.abs(square[2][0]-8); tempScore = tempScore + Math.abs(square[2][1]-1); tempScore = tempScore + Math.abs(square[2][2]-6); if(tempScore <= score) { score = tempScore; } // case 8 tempScore = 0; tempScore = tempScore + Math.abs(square[0][0]-2); tempScore = tempScore + Math.abs(square[0][1]-7); tempScore = tempScore + Math.abs(square[0][2]-6); tempScore = tempScore + Math.abs(square[1][0]-9); tempScore = tempScore + Math.abs(square[1][2]-1); tempScore = tempScore + Math.abs(square[2][0]-4); tempScore = tempScore + Math.abs(square[2][1]-3); tempScore = tempScore + Math.abs(square[2][2]-8); if(tempScore <= score) { score = tempScore; } return score; } public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ // Read square Scanner s = new Scanner(System.in); int[][] square = new int[3][3]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { square[i][j] = s.nextInt(); } } s.close(); int score = 0; // Check if solved, if not check middle if(!solved(square)) { if(square[1][1] != 5) { score = score + Math.abs(square[1][1]-5); square[1][1]=5; } } if(!solved(square)) { score = score + minScore(square); } System.out.println(score); /* for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { System.out.print(square[i][j] + " "); } System.out.println(); } */ } }