import java.util.Scanner; /** * Created by dshar on 28-07-2016. */ public class MagicSquareForming { private static int checkFamiliarityWithMagicSquare(int givenSquare[][], int magicSquare[][]){ int familiarity = 0; for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] == magicSquare[i][j]) familiarity++; } return familiarity; } public static void main(String[] args){ //set cost to zero int cost[] = new int[8]; for(int i = 0; i < cost.length; i++)cost[i] = 0; Scanner sc = new Scanner(System.in); int givenSquare[][] = new int[3][3]; // get the input from console for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) givenSquare[i][j] = sc.nextInt(); // set predefined magicSquares int firstMagicSquare[][] = {{4, 9, 2}, {3, 5 ,7}, {8, 1, 6}}; int secondMagicSquare[][] = {{8, 3, 4}, {1, 5 ,9}, {6, 7, 2}}; int thirdMagicSquare[][] = {{6, 1, 8}, {7, 5 ,3}, {2, 9, 4}}; int fourthMagicSquare[][] = {{2, 7, 6}, {9, 5 ,1}, {4, 3, 8}}; int fifthMagicSquare[][] = {{2, 9, 4}, {7, 5 ,3}, {6, 1, 8}}; int sixthMagicSquare[][] = {{6, 7, 2}, {1, 5 ,9}, {8, 3, 4}}; int seventhMagicSquare[][] = {{8, 1, 6}, {3, 5 ,7}, {4, 9, 2}}; int eighthMagicSquare[][] = {{4, 3, 8}, {9, 5 ,1}, {2, 7, 6}}; // find familiarity of each magicSquare with givenSquare int familiarity[] = new int[8]; familiarity[0] = checkFamiliarityWithMagicSquare(givenSquare, firstMagicSquare); familiarity[1] = checkFamiliarityWithMagicSquare(givenSquare, secondMagicSquare); familiarity[2] = checkFamiliarityWithMagicSquare(givenSquare, thirdMagicSquare); familiarity[3] = checkFamiliarityWithMagicSquare(givenSquare, fourthMagicSquare); familiarity[4] = checkFamiliarityWithMagicSquare(givenSquare, fifthMagicSquare); familiarity[5]= checkFamiliarityWithMagicSquare(givenSquare, sixthMagicSquare); familiarity[6] = checkFamiliarityWithMagicSquare(givenSquare, seventhMagicSquare); familiarity[7] = checkFamiliarityWithMagicSquare(givenSquare, eighthMagicSquare); // find who has maximum familiarity int whoIsMoreFamiliar = 0; int largest = familiarity[0], index = 0; for (int i = 1; i < familiarity.length; i++) { if ( familiarity[i] > largest ) { largest = familiarity[i]; index = i; } } whoIsMoreFamiliar = index + 1; // now find the cost based on most familiar magicSquare switch(whoIsMoreFamiliar){ default: //case 1: for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] != firstMagicSquare[i][j])cost[0] += Math.abs(givenSquare[i][j] - firstMagicSquare[i][j]); } //case 2: for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] != secondMagicSquare[i][j])cost[1] += Math.abs(givenSquare[i][j] - secondMagicSquare[i][j]); } //case 3: for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] != thirdMagicSquare[i][j])cost[2] += Math.abs(givenSquare[i][j] - thirdMagicSquare[i][j]); } //case 4: for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] != fourthMagicSquare[i][j])cost[3] += Math.abs(givenSquare[i][j] - fourthMagicSquare[i][j]); } //case 5: for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] != fifthMagicSquare[i][j])cost[4] += Math.abs(givenSquare[i][j] - fifthMagicSquare[i][j]); } //case 6: for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] != sixthMagicSquare[i][j])cost[5] += Math.abs(givenSquare[i][j] - sixthMagicSquare[i][j]); } //case 7: for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] != seventhMagicSquare[i][j])cost[6] += Math.abs(givenSquare[i][j] - seventhMagicSquare[i][j]); } //case 8: for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if(givenSquare[i][j] != eighthMagicSquare[i][j])cost[7] += Math.abs(givenSquare[i][j] - eighthMagicSquare[i][j]); } break; //default: //System.out.print("whoIsMoreFamiliar is not in between 1 and 4(included)"); } int minCost = cost[0]; for(int x : cost){ if(x < minCost) minCost = x; } System.out.println(minCost); } }