#include #include #include #include #include using namespace std; void FlipUp(int mq[][3], int n) { for (int i = 0; i < n - 1; ++i) { for (int j = 1; j < n; ++j) { swap(mq[i][j], mq[j][i]); } } } void FlipDown(int mq[][3], int n) { for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - 1; ++j) { swap(mq[i][j], mq[n-1-j][n-1-i]); } } } void FlipHorizontally(int mq[][3], int n) { for (int i = 0; i < n/2; ++i) { for (int j = 0 ; j < n; ++j) { swap(mq[i][j], mq[n-1-i][j]); } } } void FlipVertically(int mq[][3], int n) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n/2; ++j) { swap(mq[i][j], mq[i][n-1-j]); } } } int Distance(int a[][3], int b[][3], int n) { int dist = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dist += abs(a[i][j]-b[i][j]); } } return dist; } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ const int n = 3; int orig[n][n] = {0}; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> orig[i][j]; } } int magic_square[3][3] = {{4,9,2}, {3,5,7}, {8,1,6}}; int min_dist = 1000; min_dist = min(min_dist, Distance(orig, magic_square, n)); FlipVertically(magic_square, n); min_dist = min(min_dist, Distance(orig, magic_square, n)); FlipDown(magic_square, n); min_dist = min(min_dist, Distance(orig, magic_square, n)); FlipHorizontally(magic_square, n); min_dist = min(min_dist, Distance(orig, magic_square, n)); FlipUp(magic_square, n); min_dist = min(min_dist, Distance(orig, magic_square, n)); FlipVertically(magic_square, n); min_dist = min(min_dist, Distance(orig, magic_square, n)); FlipDown(magic_square, n); min_dist = min(min_dist, Distance(orig, magic_square, n)); FlipHorizontally(magic_square, n); min_dist = min(min_dist, Distance(orig, magic_square, n)); cout << min_dist; return 0; }