#include #include #include #include #include using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ vector tmp(3, 0); vector > s(3, tmp); for (int i = 0; i<3; i++) { for (int j = 0; j<3; j++) { cin >> s[i][j]; } } vector > magics(8); magics[0] = { 4,9,2,3,5,7,8,1,6 }; magics[1] = { 8,3,4,1,5,9,6,7,2 }; magics[2] = { 6,1,8,7,5,3,2,9,4 }; magics[3] = { 2,7,6,9,5,1,4,3,8 }; magics[4] = { 2,9,4,7,5,3,6,1,8 }; magics[5] = { 6,7,2,1,5,9,8,3,4 }; magics[6] = { 8,1,6,3,5,7,4,9,2 }; magics[7] = { 4,3,8,9,5,1,2,7,6 }; vector cost(8, 0); for (int icost = 0; icost<8; icost++) { for (int i = 0; i<3; i++) { for (int j = 0; j<3; j++) { cost[icost] += abs(magics[icost][i * 3 + j] - s[i][j]); } } } vector::iterator iter = min_element(cost.begin(), cost.end()); cout << *iter << endl; return 0; }