#include #include #include #include #include void fill_matrix(int matrix[3][3]) { for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { std::cin >> matrix[i][j]; } } } void compare(int matrix[3][3], int square[8][9]) { int cost; int smallest_cost; int k; for (int i = 0; i < 8; ++i) { cost = 0; k = 0; for (int j = 0; j < 9; ++j) { if ((j > 0) && (j % 3 == 0)) { ++k; } if (matrix[k][j % 3] != square[i][j]) { cost += abs(matrix[k][j % 3] - square[i][j]); } } if (i == 0) { smallest_cost = cost; } if (cost < smallest_cost) { smallest_cost = cost; } } std::cout << smallest_cost; } int main() { int matrix[3][3]; fill_matrix(matrix); int squares[8][9] = {2, 9, 4, 7, 5, 3, 6, 1, 8, 2, 7, 6, 9, 5, 1, 4, 3, 8, 4, 3, 8, 9, 5, 1, 2, 7, 6, 4, 9, 2, 3, 5, 7, 8, 1, 6, 6, 7, 2, 1, 5, 9, 8, 3, 4, 6, 1, 8, 7, 5, 3, 2, 9, 4, 8, 1, 6, 3, 5, 7, 4, 9, 2, 8, 3, 4, 1, 5, 9, 6, 7, 2}; compare(matrix, squares); }