Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int sq[3][3];
- int findSum()
- {
- int i;
- for (i=0;i<3;i++)
- {
- ///Check if row is full
- if (sq[i][0] != 0 && sq[i][1] != 0 && sq[i][2] != 0)
- return sq[i][0] + sq[i][1] + sq[i][2];
- ///Check if column is full
- if (sq[0][i] != 0 && sq[1][i] != 0 && sq[2][i] != 0)
- return sq[0][i] + sq[1][i] + sq[2][i];
- }
- ///Check main diagonal
- if (sq[0][0] != 0 && sq[1][1] != 0 && sq[2][2] != 0)
- return sq[0][0] + sq[1][1] + sq[2][2];
- else ///Secondary diagonal
- return sq[0][2] + sq[1][1] + sq[2][0];
- }
- bool correctSquare()
- {
- int sum = findSum();
- int i;
- ///Check diagonals
- if (sq[0][0] + sq[1][1] + sq[2][2] != sum)
- return false;
- if (sq[0][2] + sq[1][1] + sq[2][0] != sum)
- return false;
- for (i=0;i<3;i++)
- {
- ///Check rows
- if (sq[i][0] + sq[i][1] + sq[i][2] != sum)
- return false;
- ///Check columns
- if (sq[0][i] + sq[1][i] + sq[2][i] != sum)
- return false;
- }
- return true;
- }
- int main()
- {
- int i,j;
- ///Input
- for (i=0;i<3;i++)
- {
- for (j=0;j<3;j++)
- {
- cin>>sq[i][j];
- }
- }
- if ( (sq[0][0] == 0 && sq[1][1] == 0 && sq[2][2] == 0)
- || (sq[0][2] == 0 && sq[1][1] == 0 && sq[2][0] == 0) ) ///Empty diagonal
- {
- bool flip = false;
- if (sq[0][2] == 0) ///Secondary diagonal is empty
- {
- ///Flip square horizontally so you don't solve two cases separately
- for (i=0;i<3;i++)
- {
- swap(sq[i][0], sq[i][2]);
- }
- flip = true;
- }
- for (i=1;i<=20000;i++) ///Iterate over top-left square value
- {
- sq[0][0] = i;
- sq[1][1] = (sq[0][0] + sq[0][1] + sq[0][2]) - (sq[1][0] + sq[1][2]);
- sq[2][2] = (sq[0][0] + sq[0][1] + sq[0][2]) - (sq[2][0] + sq[2][1]);
- if (correctSquare())
- break;
- }
- if (i > 20000) ///No solution
- {
- cout<<-1<<endl;
- return 0;
- }
- if (flip)
- {
- ///Flip back so output is correct
- for (i=0;i<3;i++)
- {
- swap(sq[i][0], sq[i][2]);
- }
- }
- }
- else ///General case (no diagonal is completely empty)
- {
- int magicSum = findSum(); ///Find the sum required
- ///Fill rows
- for (i=0;i<3;i++)
- {
- if (sq[i][0] == 0 && sq[i][1] != 0 && sq[i][2] != 0)
- sq[i][0] = magicSum - sq[i][1] - sq[i][2];
- else if (sq[i][0] != 0 && sq[i][1] == 0 && sq[i][2] != 0)
- sq[i][1] = magicSum - sq[i][0] - sq[i][2];
- else if (sq[i][0] != 0 && sq[i][1] != 0 && sq[i][2] == 0)
- sq[i][2] = magicSum - sq[i][0] - sq[i][1];
- }
- ///Fill columns
- for (i=0;i<3;i++)
- {
- if (sq[0][i] == 0 && sq[1][i] != 0 && sq[2][i] != 0)
- sq[0][i] = magicSum - sq[1][i] - sq[2][i];
- else if (sq[0][i] != 0 && sq[1][i] == 0 && sq[2][i] != 0)
- sq[1][i] = magicSum - sq[0][i] - sq[2][i];
- else if (sq[0][i] != 0 && sq[1][i] != 0 && sq[2][i] == 0)
- sq[2][i] = magicSum - sq[0][i] - sq[1][i];
- }
- ///Make sure result is consistent
- if (!correctSquare())
- {
- cout<<-1<<endl;
- return 0;
- }
- }
- ///Print result
- for (i=0;i<3;i++)
- {
- for (j=0;j<3;j++)
- {
- cout<<sq[i][j];
- if (j != 2)
- cout<<" ";
- }
- cout<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment