#include <bits/stdc++.h>
using namespace std;
const int N = 81, P = 9;
int block[][ P ] = {
{ 0, 0, 0, 1, 1, 1, 2, 2, 2 },
{ 0, 0, 0, 1, 1, 1, 2, 2, 2 },
{ 0, 0, 0, 1, 1, 1, 2, 2, 2 },
{ 3, 3, 3, 4, 4, 4, 5, 5, 5 },
{ 3, 3, 3, 4, 4, 4, 5, 5, 5 },
{ 3, 3, 3, 4, 4, 4, 5, 5, 5 },
{ 6, 6, 6, 7, 7, 7, 8, 8, 8 },
{ 6, 6, 6, 7, 7, 7, 8, 8, 8 },
{ 6, 6, 6, 7, 7, 7, 8, 8, 8 } };
class sudoku_t: vector< int >
{
int cell[ P ][ P ];
typedef bitset< P > bitset_t;
array< bitset_t, P > row, col, blk;
bool set_cell( int i, int j, int k )
{
int b = block[ i ][ j ], l = k - 1;
if ( row[ i ][ l ] or col[ j ][ l ] or blk[ b ][ l ] )
return false;
else
{
push_back( cell[ i ][ j ] ), cell[ i ][ j ] = k;
return row[ i ][ l ] = col[ j ][ l ] = blk[ b ][ l ] = true;
}
}
void reset_cell( int i, int j )
{
int b = block[ i ][ j ], k = cell[ i ][ j ], l = k - 1;
cell[ i ][ j ] = back(), pop_back(), row[ i ][ l ] = col[ j ][ l ] = blk[ b ][ l ] = false;
}
public:
sudoku_t()
{
for( int i = 0; i < P; i++ )
for( int j = 0; j < P; j++ )
{
int k; cin >> k, cell[ i ][ j ] = 0;
if ( k > 0 and not set_cell( i, j, k ) )
{
cout << "invalid initial state cell(",
cout << i << ',' << j << ") = " << k << endl;
exit( 1 );
}
}
}
bool solved( const int row = 0, const int col = 0 )
{
int next_col = col, next_row = row;
if ( ++next_col == P )
next_col = 0, ++next_row;
if ( cell[ row ][ col ] != 0 )
return size() == N ? true : solved( next_row, next_col );
for( int k = 1; k <= P; k++ )
if ( set_cell( row, col, k ) )
{
if ( size() == N or solved( next_row, next_col ) )
return true;
else
reset_cell( row, col );
}
return false;
}
void write() const
{
for( int i = 0; i < P; i++, cout << '\n' )
for( int j = 0; j < P; j++ )
cout << cell[ i ][ j ] << ' ';
}
};
int main()
{
ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );
sudoku_t sudoku;
if ( sudoku.solved() )
sudoku.write();
else
cout << "No solution";
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE4gPSA4MSwgUCA9IDk7CgppbnQgYmxvY2tbXVsgUCBdID0gewogICAgeyAwLCAwLCAwLCAxLCAxLCAxLCAyLCAyLCAyIH0sCiAgICB7IDAsIDAsIDAsIDEsIDEsIDEsIDIsIDIsIDIgfSwKICAgIHsgMCwgMCwgMCwgMSwgMSwgMSwgMiwgMiwgMiB9LAogICAgeyAzLCAzLCAzLCA0LCA0LCA0LCA1LCA1LCA1IH0sCiAgICB7IDMsIDMsIDMsIDQsIDQsIDQsIDUsIDUsIDUgfSwKICAgIHsgMywgMywgMywgNCwgNCwgNCwgNSwgNSwgNSB9LAogICAgeyA2LCA2LCA2LCA3LCA3LCA3LCA4LCA4LCA4IH0sCiAgICB7IDYsIDYsIDYsIDcsIDcsIDcsIDgsIDgsIDggfSwKICAgIHsgNiwgNiwgNiwgNywgNywgNywgOCwgOCwgOCB9IH07CgpjbGFzcyBzdWRva3VfdDogdmVjdG9yPCBpbnQgPgp7CiAgICBpbnQgY2VsbFsgUCBdWyBQIF07CgogICAgdHlwZWRlZiBiaXRzZXQ8IFAgPiAgICAgICBiaXRzZXRfdDsKCiAgICBhcnJheTwgYml0c2V0X3QsIFAgPiByb3csIGNvbCwgYmxrOwoKICAgIGJvb2wgc2V0X2NlbGwoIGludCBpLCBpbnQgaiwgaW50IGsgKQogICAgewogICAgICAgIGludCBiID0gYmxvY2tbIGkgXVsgaiBdLCBsID0gayAtIDE7CgogICAgICAgIGlmICggcm93WyBpIF1bIGwgXSBvciBjb2xbIGogXVsgbCBdIG9yIGJsa1sgYiBdWyBsIF0gKQogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgcHVzaF9iYWNrKCBjZWxsWyBpIF1bIGogXSApLCBjZWxsWyBpIF1bIGogXSA9IGs7CgogICAgICAgICAgICByZXR1cm4gcm93WyBpIF1bIGwgXSA9IGNvbFsgaiBdWyBsIF0gPSBibGtbIGIgXVsgbCBdID0gdHJ1ZTsKICAgICAgICB9CiAgICB9CgogICAgdm9pZCByZXNldF9jZWxsKCBpbnQgaSwgaW50IGogKQogICAgewogICAgICAgIGludCBiID0gYmxvY2tbIGkgXVsgaiBdLCBrID0gY2VsbFsgaSBdWyBqIF0sIGwgPSBrIC0gMTsKCiAgICAgICAgY2VsbFsgaSBdWyBqIF0gPSBiYWNrKCksIHBvcF9iYWNrKCksIHJvd1sgaSBdWyBsIF0gPSBjb2xbIGogXVsgbCBdID0gYmxrWyBiIF1bIGwgXSA9IGZhbHNlOwogICAgfQoKcHVibGljOgoKICAgIHN1ZG9rdV90KCkKICAgIHsKICAgICAgICBmb3IoIGludCBpID0gMDsgaSA8IFA7IGkrKyApCiAgICAgICAgICAgIGZvciggaW50IGogPSAwOyBqIDwgUDsgaisrICkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaW50IGs7IGNpbiA+PiBrLCBjZWxsWyBpIF1bIGogXSA9IDA7CgogICAgICAgICAgICAgICAgaWYgKCBrID4gMCBhbmQgbm90IHNldF9jZWxsKCBpLCBqLCBrICkgKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgImludmFsaWQgaW5pdGlhbCBzdGF0ZSBjZWxsKCIsCiAgICAgICAgICAgICAgICAgICAgY291dCA8PCBpIDw8ICcsJyA8PCBqIDw8ICIpID0gIiA8PCBrIDw8IGVuZGw7CiAgICAgICAgICAgICAgICAgICAgZXhpdCggMSApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICB9CgogICAgYm9vbCBzb2x2ZWQoIGNvbnN0IGludCByb3cgPSAwLCBjb25zdCBpbnQgY29sID0gMCApCiAgICB7CiAgICAgICAgaW50IG5leHRfY29sID0gY29sLCBuZXh0X3JvdyA9IHJvdzsKCiAgICAgICAgaWYgKCArK25leHRfY29sID09IFAgKQogICAgICAgICAgICBuZXh0X2NvbCA9IDAsICsrbmV4dF9yb3c7CgogICAgICAgIGlmICggY2VsbFsgcm93IF1bIGNvbCBdICE9IDAgKQogICAgICAgICAgICByZXR1cm4gc2l6ZSgpID09IE4gPyB0cnVlIDogc29sdmVkKCBuZXh0X3JvdywgbmV4dF9jb2wgKTsKCiAgICAgICAgZm9yKCBpbnQgayA9IDE7IGsgPD0gUDsgaysrICkKICAgICAgICAgICAgaWYgKCBzZXRfY2VsbCggcm93LCBjb2wsIGsgKSApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmICggc2l6ZSgpID09IE4gb3Igc29sdmVkKCBuZXh0X3JvdywgbmV4dF9jb2wgKSApCiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgcmVzZXRfY2VsbCggcm93LCBjb2wgKTsKICAgICAgICAgICAgfQoKICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9CgogICAgdm9pZCB3cml0ZSgpIGNvbnN0CiAgICB7CiAgICAgICAgZm9yKCBpbnQgaSA9IDA7IGkgPCBQOyBpKyssIGNvdXQgPDwgJ1xuJyApCiAgICAgICAgICAgIGZvciggaW50IGogPSAwOyBqIDwgUDsgaisrICkKICAgICAgICAgICAgICAgIGNvdXQgPDwgY2VsbFsgaSBdWyBqIF0gPDwgJyAnOwogICAgfQp9OwoKaW50IG1haW4oKQp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKCBmYWxzZSApLCBjaW4udGllKCBudWxscHRyICksIGNvdXQudGllKCBudWxscHRyICk7CgogICAgc3Vkb2t1X3Qgc3Vkb2t1OwoKICAgIGlmICggc3Vkb2t1LnNvbHZlZCgpICkKICAgICAgICBzdWRva3Uud3JpdGUoKTsKICAgIGVsc2UKICAgICAgICBjb3V0IDw8ICJObyBzb2x1dGlvbiI7Cn0K