forked from JGEC-Winter-of-Code/JWoC_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Magic_Square.cpp
127 lines (115 loc) · 2.42 KB
/
Magic_Square.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define SZ(x) ((int)(x.size()))
#define fi first
#define se second
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define DBG cerr << "debug here" << endl;
#define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl;
typedef long long ll;
const int INF = 1e9;
const int MINV = 1;
const int MAXV = 10;
vector<vi> magic_squares;
void print_magic_square(vi v)
{
FOR(i, 3)
{
FOR(j, 3)
{
cout << v[3 * i + j] << " ";
}
cout << endl;
}
}
bool is_magic_square(vi v)
{
int a[3][3];
FOR(i, 3)
{
FOR(j, 3)
{
a[i][j] = v[3 * i + j];
}
}
int s = 0;
FOR(j, 3) s += a[0][j];
REP(i, 1, 2)
{
int tmp = 0;
FOR(j, 3) tmp += a[i][j];
if(tmp != s) return 0;
}
FOR(j, 3)
{
int tmp = 0;
FOR(i, 3) tmp += a[i][j];
if(tmp != s) return 0;
}
int tmp = 0;
FOR(i, 3) tmp += a[i][i];
if(tmp != s) return 0;
tmp = 0;
FOR(i, 3) tmp += a[2 - i][i];
if(tmp != s) return 0;
return 1;
}
void find_magic_squares()
{
vi v(9);
FOR(i, 9) v[i] = i + 1;
do
{
if(is_magic_square(v))
{
magic_squares.pb(v);
}
} while(next_permutation(ALL(v)));
}
int diff(vi a, vi b)
{
int res = 0;
FOR(i, 9) res += abs(a[i] - b[i]);
return res;
}
int main()
{
ios_base::sync_with_stdio(0);
vi v(9);
FOR(i, 9) cin >> v[i];
int res = INF;
find_magic_squares();
FOR(i, magic_squares.size())
{
//print_magic_square(magic_squares[i]);
//cout << "########" << endl;
REMIN(res, diff(v, magic_squares[i]));
}
cout << res << endl;
}