-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
47 lines (36 loc) · 1.54 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
namespace Forming_a_Magic_Square {
class Program {
static int FormingMagicSquare(int[][] s) {
int[, , ] allPossibleSolutions = new int[8, 3, 3] {
{ { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } },
{ { 6, 1, 8 }, { 7, 5, 3 }, { 2, 9, 4 } },
{ { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } },
{ { 2, 9, 4 }, { 7, 5, 3 }, { 6, 1, 8 } },
{ { 8, 3, 4 }, { 1, 5, 9 }, { 6, 7, 2 } },
{ { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 } },
{ { 6, 7, 2 }, { 1, 5, 9 }, { 8, 3, 4 } },
{ { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } }
};
int minPermutationCost = int.MaxValue;
for (int p = 0; p < allPossibleSolutions.GetLength(0); p++) {
int permutationCost = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
permutationCost += Math.Abs(s[i][j] - allPossibleSolutions[p,i,j]);
}
}
minPermutationCost = Math.Min(minPermutationCost, permutationCost);
}
return minPermutationCost;
}
static void Main(string[] args) {
int[][] s = new int[3][];
for (int i = 0; i < 3; i++) {
s[i] = Array.ConvertAll(Console.ReadLine().Split(' '), sTemp => Convert.ToInt32(sTemp));
}
int result = FormingMagicSquare(s);
Console.WriteLine(result);
}
}
}