Guest User

Untitled

a guest
Apr 21st, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sq[3][3];
  5.  
  6. int findSum()
  7. {
  8. int i;
  9.  
  10. for (i=0;i<3;i++)
  11. {
  12. ///Check if row is full
  13. if (sq[i][0] != 0 && sq[i][1] != 0 && sq[i][2] != 0)
  14. return sq[i][0] + sq[i][1] + sq[i][2];
  15.  
  16. ///Check if column is full
  17. if (sq[0][i] != 0 && sq[1][i] != 0 && sq[2][i] != 0)
  18. return sq[0][i] + sq[1][i] + sq[2][i];
  19. }
  20.  
  21. ///Check main diagonal
  22. if (sq[0][0] != 0 && sq[1][1] != 0 && sq[2][2] != 0)
  23. return sq[0][0] + sq[1][1] + sq[2][2];
  24. else ///Secondary diagonal
  25. return sq[0][2] + sq[1][1] + sq[2][0];
  26. }
  27.  
  28. bool correctSquare()
  29. {
  30. int sum = findSum();
  31. int i;
  32.  
  33. ///Check diagonals
  34. if (sq[0][0] + sq[1][1] + sq[2][2] != sum)
  35. return false;
  36. if (sq[0][2] + sq[1][1] + sq[2][0] != sum)
  37. return false;
  38.  
  39. for (i=0;i<3;i++)
  40. {
  41. ///Check rows
  42. if (sq[i][0] + sq[i][1] + sq[i][2] != sum)
  43. return false;
  44. ///Check columns
  45. if (sq[0][i] + sq[1][i] + sq[2][i] != sum)
  46. return false;
  47. }
  48.  
  49. return true;
  50. }
  51.  
  52. int main()
  53. {
  54. int i,j;
  55.  
  56. ///Input
  57. for (i=0;i<3;i++)
  58. {
  59. for (j=0;j<3;j++)
  60. {
  61. cin>>sq[i][j];
  62. }
  63. }
  64.  
  65. if ( (sq[0][0] == 0 && sq[1][1] == 0 && sq[2][2] == 0)
  66. || (sq[0][2] == 0 && sq[1][1] == 0 && sq[2][0] == 0) ) ///Empty diagonal
  67. {
  68. bool flip = false;
  69.  
  70. if (sq[0][2] == 0) ///Secondary diagonal is empty
  71. {
  72. ///Flip square horizontally so you don't solve two cases separately
  73. for (i=0;i<3;i++)
  74. {
  75. swap(sq[i][0], sq[i][2]);
  76. }
  77. flip = true;
  78. }
  79.  
  80. for (i=1;i<=20000;i++) ///Iterate over top-left square value
  81. {
  82. sq[0][0] = i;
  83. sq[1][1] = (sq[0][0] + sq[0][1] + sq[0][2]) - (sq[1][0] + sq[1][2]);
  84. sq[2][2] = (sq[0][0] + sq[0][1] + sq[0][2]) - (sq[2][0] + sq[2][1]);
  85.  
  86. if (correctSquare())
  87. break;
  88. }
  89.  
  90. if (i > 20000) ///No solution
  91. {
  92. cout<<-1<<endl;
  93. return 0;
  94. }
  95.  
  96. if (flip)
  97. {
  98. ///Flip back so output is correct
  99. for (i=0;i<3;i++)
  100. {
  101. swap(sq[i][0], sq[i][2]);
  102. }
  103. }
  104. }
  105. else ///General case (no diagonal is completely empty)
  106. {
  107. int magicSum = findSum(); ///Find the sum required
  108.  
  109. ///Fill rows
  110. for (i=0;i<3;i++)
  111. {
  112. if (sq[i][0] == 0 && sq[i][1] != 0 && sq[i][2] != 0)
  113. sq[i][0] = magicSum - sq[i][1] - sq[i][2];
  114. else if (sq[i][0] != 0 && sq[i][1] == 0 && sq[i][2] != 0)
  115. sq[i][1] = magicSum - sq[i][0] - sq[i][2];
  116. else if (sq[i][0] != 0 && sq[i][1] != 0 && sq[i][2] == 0)
  117. sq[i][2] = magicSum - sq[i][0] - sq[i][1];
  118. }
  119.  
  120. ///Fill columns
  121. for (i=0;i<3;i++)
  122. {
  123. if (sq[0][i] == 0 && sq[1][i] != 0 && sq[2][i] != 0)
  124. sq[0][i] = magicSum - sq[1][i] - sq[2][i];
  125. else if (sq[0][i] != 0 && sq[1][i] == 0 && sq[2][i] != 0)
  126. sq[1][i] = magicSum - sq[0][i] - sq[2][i];
  127. else if (sq[0][i] != 0 && sq[1][i] != 0 && sq[2][i] == 0)
  128. sq[2][i] = magicSum - sq[0][i] - sq[1][i];
  129. }
  130.  
  131. ///Make sure result is consistent
  132. if (!correctSquare())
  133. {
  134. cout<<-1<<endl;
  135. return 0;
  136. }
  137. }
  138.  
  139. ///Print result
  140. for (i=0;i<3;i++)
  141. {
  142. for (j=0;j<3;j++)
  143. {
  144. cout<<sq[i][j];
  145.  
  146. if (j != 2)
  147. cout<<" ";
  148. }
  149. cout<<endl;
  150. }
  151.  
  152. return 0;
  153. }
Add Comment
Please, Sign In to add comment