using System; using System.Collections.Generic; using System.IO; class Solution { /* There are only 8 distinct magic squares of size 3x3. Also, magic squares have 2 properties: Center of magic square of size 3x3 is always 5. No corner cell can contain an odd integer. */ private static int ComputeCost(int[] inputArr, int[] checkArr){ int cost = 0; for (int i = 0; i < 9; i++){ cost += Math.Abs(inputArr[i] - checkArr[i]); } return cost; } private static int[] GetSolutionArray(int solutionArray){ int[] sol0 = {2,9,4, 7,5,3, 6,1,8}; int[] sol1 = {2,7,6, 9,5,1, 4,3,8}; int[] sol2 = {4,9,2, 3,5,7, 8,1,6}; int[] sol3 = {8,3,4, 1,5,9, 6,7,2}; int[] sol4 = {6,1,8, 7,5,3, 2,9,4}; int[] sol5 = {6,7,2, 1,5,9, 8,3,4}; int[] sol6 = {4,3,8, 9,5,1, 2,7,6}; int[] sol7 = {8,1,6, 3,5,7, 4,9,2}; switch (solutionArray){ case 0: return sol0; case 1: return sol1; case 2: return sol2; case 3: return sol3; case 4: return sol4; case 5: return sol5; case 6: return sol6; case 7: return sol7; default: return null; } } static void Main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ string[] arr_temp = Console.ReadLine().Split(' '); int[] row1Arr = Array.ConvertAll(arr_temp,Int32.Parse); arr_temp = Console.ReadLine().Split(' '); int[] row2Arr = Array.ConvertAll(arr_temp,Int32.Parse); arr_temp = Console.ReadLine().Split(' '); int[] row3Arr = Array.ConvertAll(arr_temp,Int32.Parse); int[] inputArr = new int[9]; for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ if (i == 0) inputArr[i*3 + j] = row1Arr[j]; if (i == 1) inputArr[i*3 + j] = row2Arr[j]; if (i == 2) inputArr[i*3 + j] = row3Arr[j]; } } int cost = 999; for (int i = 0; i < 8; i++){ int [] solArr = GetSolutionArray(i); int tempCost = ComputeCost(inputArr, solArr); if (tempCost < cost) cost = tempCost; } Console.Write(cost.ToString()); } }