fork download
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. std::pair<int, int> calculate_distance(int value, int x_position, int y_position) {
  7. int index = value - 1;
  8.  
  9. int distance = -1 * (y_position * 3 + x_position - index);
  10.  
  11. int target_x = distance % 3;
  12. int target_y = distance / 3;
  13.  
  14. if (x_position + target_x < 0) { // Underflow x
  15. target_x += 3;
  16. target_y--;
  17. }
  18.  
  19. return std::pair<int, int>(target_x, target_y);
  20. }
  21.  
  22. int main() {
  23. // The representation of movements
  24. std::pair<int, int> puzzle [3][3] { };
  25.  
  26. for (auto i = 0, a = 0, b = 0, c = 0; i < 3; ++i) {
  27. cin >> a >> b >> c;
  28. puzzle[0][i] = calculate_distance(a, 0, i);
  29. puzzle[1][i] = calculate_distance(b, 1, i);
  30. puzzle[2][i] = calculate_distance(c, 2, i);
  31. }
  32.  
  33. for (auto i = 0; i < 3; ++i) {
  34. cout << puzzle[0][i].first << "," << puzzle[0][i].second << ' ';
  35. cout << puzzle[1][i].first << "," << puzzle[1][i].second << ' ';
  36. cout << puzzle[2][i].first << "," << puzzle[2][i].second << endl;
  37. }
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 4524KB
stdin
3 5 2
7 8 9
1 6 4
stdout
2,0 0,1 -1,0
0,1 0,1 0,1
0,-2 1,-1 -2,-1