#include #include #include #include #include using namespace std; int cost(int m[3][3], int n[3][3]){ int c = 0; for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ c+= abs(m[i][j] - n[i][j]); } } return c; } void reflect_about_x(int m[3][3], int n[3][3]){ for(int i=0; i<3; i++) m[1][i] = n[1][i]; for(int i=0; i<3; i++){ m[2][i] = n[0][i]; m[0][i] = n[2][i]; } return; } void reflect_about_y(int m[3][3], int n[3][3]){ for(int i=0; i<3; i++) m[i][1] = n[i][1]; for(int i=0; i<3; i++){ m[i][0] = n[i][2]; m[i][2] = n[i][0]; } return; } void copy(int m[3][3], int n[3][3]){ for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ m[i][j] = n[i][j]; } } } void rotate_clockwise(int m[3][3]){ int n[3][3]; n[0][0] = m[2][0]; n[0][1] = m[1][0]; n[0][2] = m[0][0]; n[1][0] = m[2][1]; n[1][1] = m[1][1]; n[1][2] = m[0][1]; n[2][0] = m[2][2]; n[2][1] = m[1][2]; n[2][2] = m[0][2]; copy(m,n); return; } void print(int m[3][3]){ cout<<"******************"; for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ cout<>test[i][j]; } } int m[3][3]; int curr[3][3]; m[0][0] = 2; m[0][1] = 7; m[0][2] = 6; m[1][0] = 9; m[1][1] = 5; m[1][2] = 1; m[2][0] = 4; m[2][1] = 3; m[2][2] = 8; int curr_cost; int min_cost = cost(m,test); reflect_about_x(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_y(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; rotate_clockwise(m); curr_cost = cost(m,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_x(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_y(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; rotate_clockwise(m); curr_cost = cost(m,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_x(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_y(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; rotate_clockwise(m); curr_cost = cost(m,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_x(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_y(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; rotate_clockwise(m); curr_cost = cost(m,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_x(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; reflect_about_y(curr,m); curr_cost = cost(curr,test); if(curr_cost < min_cost) min_cost = curr_cost; cout<