using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { //- initialize ALL POSSIBLE magic squares int[] m1 = new int[8]; int[] m2 = new int[8]; int[] m3 = new int[8]; int[] m4 = new int[8]; int[] m5 = new int[8]; int[] m6 = new int[8]; int[] m7 = new int[8]; int[] m8 = new int[8]; int[] m9 = new int[8]; m1[0] = 8; m2[0] = 1; m3[0] = 6; m4[0] = 3; m5[0] = 5; m6[0] = 7; m7[0] = 4; m8[0] = 9; m9[0] = 2; m1[1] = 4; m2[1] = 3; m3[1] = 8; m4[1] = 9; m5[1] = 5; m6[1] = 1; m7[1] = 2; m8[1] = 7; m9[1] = 6; m1[2] = 2; m2[2] = 9; m3[2] = 4; m4[2] = 7; m5[2] = 5; m6[2] = 3; m7[2] = 6; m8[2] = 1; m9[2] = 8; m1[3] = 6; m2[3] = 7; m3[3] = 2; m4[3] = 1; m5[3] = 5; m6[3] = 9; m7[3] = 8; m8[3] = 3; m9[3] = 4; m1[4] = 6; m2[4] = 1; m3[4] = 8; m4[4] = 7; m5[4] = 5; m6[4] = 3; m7[4] = 2; m8[4] = 9; m9[4] = 4; m1[5] = 8; m2[5] = 3; m3[5] = 4; m4[5] = 1; m5[5] = 5; m6[5] = 9; m7[5] = 6; m8[5] = 7; m9[5] = 2; m1[6] = 4; m2[6] = 9; m3[6] = 2; m4[6] = 3; m5[6] = 5; m6[6] = 7; m7[6] = 8; m8[6] = 1; m9[6] = 6; m1[7] = 2; m2[7] = 7; m3[7] = 6; m4[7] = 9; m5[7] = 5; m6[7] = 1; m7[7] = 4; m8[7] = 3; m9[7] = 8; /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ //- get the magic square string topRowString = Console.ReadLine(); string middleRowString = Console.ReadLine(); string bottomRowString = Console.ReadLine(); string[] topRowStringArray = topRowString.Split(' '); string[] middleRowStringArray = middleRowString.Split(' '); string[] bottomRowStringArray = bottomRowString.Split(' '); int[] topRowArray = new int[3]; int[] middleRowArray = new int[3]; int[] bottomRowArray = new int[3]; int minimumCost = 0; topRowArray[0] = Convert.ToInt32(topRowStringArray[0]); topRowArray[1] = Convert.ToInt32(topRowStringArray[1]); topRowArray[2] = Convert.ToInt32(topRowStringArray[2]); middleRowArray[0] = Convert.ToInt32(middleRowStringArray[0]); middleRowArray[1] = Convert.ToInt32(middleRowStringArray[1]); middleRowArray[2] = Convert.ToInt32(middleRowStringArray[2]); bottomRowArray[0] = Convert.ToInt32(bottomRowStringArray[0]); bottomRowArray[1] = Convert.ToInt32(bottomRowStringArray[1]); bottomRowArray[2] = Convert.ToInt32(bottomRowStringArray[2]); minimumCost = GetMinimumCost(topRowArray, middleRowArray, bottomRowArray, m1, m2, m3, m4, m5, m6, m7, m8, m9); Console.WriteLine(minimumCost); } static int GetMinimumCost (int [] topRowArray, int[] middleRowArray, int[] bottomRowArray, int[] m1, int[] m2, int[] m3, int[] m4, int[] m5, int[] m6, int[] m7, int[] m8, int[] m9) { int minimumCost = 72; int cost = 0; for (int magicSquareIndex = 0; magicSquareIndex < 8; magicSquareIndex++) { cost = Math.Abs(topRowArray[0] - m1[magicSquareIndex]) + Math.Abs(topRowArray[1] - m2[magicSquareIndex]) + Math.Abs(topRowArray[2] - m3[magicSquareIndex]) + Math.Abs(middleRowArray[0] - m4[magicSquareIndex]) + Math.Abs(middleRowArray[1] - m5[magicSquareIndex]) + Math.Abs(middleRowArray[2] - m6[magicSquareIndex]) + Math.Abs(bottomRowArray[0] - m7[magicSquareIndex]) + Math.Abs(bottomRowArray[1] - m8[magicSquareIndex]) + Math.Abs(bottomRowArray[2] - m9[magicSquareIndex]); if (cost < minimumCost) { minimumCost = cost; } } return minimumCost; } }