#include #include #include #include #include using namespace std; int Diff(vector inputSquare, vector comparedSquare) { //Input: Takes in 2 squares //Output: Returns the diff between the two, in cost. int value = 0; for (int i=0; i<9; i++) { value = value + abs(inputSquare[i]-comparedSquare[i]); } return value; } int main() { //Input int number; vector inputSquare; for (int i=0; i<9; i++) { cin >> number; inputSquare.push_back(number); } //Define some stuffs vector< vector < int > > magicSquares; vector square1 {6,7,2,1,5,9,8,3,4}; vector square2 {8,3,4,1,5,9,6,7,2}; vector square3 {2,7,6,9,5,1,4,3,8}; vector square4 {4,3,8,9,5,1,2,7,6}; vector square5 {8,1,6,3,5,7,4,9,2}; vector square6 {6,1,8,7,5,3,2,9,4}; vector square7 {4,9,2,3,5,7,8,1,6}; vector square8 {2,9,4,7,5,3,6,1,8}; magicSquares.push_back(square1); magicSquares.push_back(square2); magicSquares.push_back(square3); magicSquares.push_back(square4); magicSquares.push_back(square5); magicSquares.push_back(square6); magicSquares.push_back(square7); magicSquares.push_back(square8); //TA-DA! //Algorithm int minCost = 9001; //A big number, a lil over 9000. for (vector square: magicSquares) { int minDiff = Diff(inputSquare, square); if (minDiff < minCost) { minCost = minDiff; } } //Output cout << minCost; return 0; }