#include #include #include #include #include #include #include using namespace std; int calculateCost(int arr1[3][3], int arr2[3][3]) { int cost = 0; for (int i=0;i<3;++i) { for (int j=0;j<3;++j) { //cout << arr1[i][j] << " - " << arr2[i][j] << " = "; //cout << abs(arr1[i][j] - arr2[i][j]) << endl; cost += abs(arr1[i][j] - arr2[i][j]); } //cout << endl; } //cout << "Total Cost : " << cost << endl << endl; return cost; } int main() { int matrix[3][3]; for (int i=0;i<3;++i) { string values; getline(cin, values); istringstream ss(values); for (int j=0;j<3;++j) { int num; ss >> num; matrix[i][j] = num; } } int magicSq1[3][3] = {{8,1,6},{3,5,7},{4,9,2}}; int magicSq2[3][3] = {{4,9,2},{3,5,7},{8,1,6}}; int magicSq3[3][3] = {{8,3,4},{1,5,9},{6,7,2}}; int magicSq4[3][3] = {{6,7,2},{1,5,9},{8,3,4}}; int magicSq5[3][3] = {{2,9,4},{7,5,3},{6,1,8}}; int magicSq6[3][3] = {{6,1,8},{7,5,3},{2,9,4}}; int magicSq7[3][3] = {{4,3,8},{9,5,1},{2,7,6}}; int magicSq8[3][3] = {{2,7,6},{9,5,1},{4,3,8}}; vector costs; costs.push_back(calculateCost(matrix, magicSq1)); costs.push_back(calculateCost(matrix, magicSq2)); costs.push_back(calculateCost(matrix, magicSq3)); costs.push_back(calculateCost(matrix, magicSq4)); costs.push_back(calculateCost(matrix, magicSq5)); costs.push_back(calculateCost(matrix, magicSq6)); costs.push_back(calculateCost(matrix, magicSq7)); costs.push_back(calculateCost(matrix, magicSq8)); int minCost = costs.at(0); for(int i=0;i<8;++i) { //cout << costs.at(i) << " "; if (minCost > costs.at(i)) { minCost = costs.at(i); } } cout << minCost; /* for (int i=0;i<3;++i) { for (int j=0;j<3;++j) { cout << magicSq1[i][j] << " "; } cout << endl; }*/ return 0; }