#include using namespace std; int Sq[25][25]; int n, dim; int HasR[25], HasC[25], HasS[25]; int Spot[25][25]; void Preprocess() { for(int i = 0; i < dim; ++i) for(int j = 0; j < dim; ++j) { int a = i / n; int b = j / n; Sq[i][j] = a * n + b; } for(int i = 0; i < dim; ++i, cerr << endl) for(int j = 0; j < dim; ++j) cerr << Sq[i][j] << " "; } bool CompFree[1 << 25]; int MemFree[1 << 25]; int Free(int mask) { if(CompFree[mask]) return MemFree[mask]; CompFree[mask] = true; MemFree[mask] = dim - __builtin_popcount(mask); return MemFree[mask]; } void Out(int x) { for(int i = 0; i < dim; ++i) { if(x & (1 << i)) cerr << 1; else cerr << 0; } cerr << " "; } void Back(int left) { if(left == 0) { // Output for(int i = 0; i < dim; ++i) { for(int j = 0; j < dim; ++j) cout << Spot[i][j] << " "; cout << endl; } cout.flush(); exit(0); } int ci = -1, cj = -1, cd = 500; for(int i = 0; i < dim; ++i) { for(int j = 0; j < dim; ++j) { if(Spot[i][j] > 0) continue; int busy = (HasR[i] | HasC[j] | HasS[Sq[i][j]]); int deg = Free(busy); if(deg == 0) { return; } if(cd > deg) { cd = deg; ci = i; cj = j; } } } assert(ci != -1); int busy = (HasR[ci] | HasC[cj] | HasS[Sq[ci][cj]]); assert(Spot[ci][cj] == 0); for(int val = 0; val < dim; ++val) { if((busy & (1 << val)) == 0) { HasR[ci] ^= (1 << val); HasC[cj] ^= (1 << val); HasS[Sq[ci][cj]] ^= (1 << val); // Out(HasR[ci]); Out(HasC[cj]); Out(HasS[Sq[ci][cj]]); cerr << endl; Spot[ci][cj] = val + 1; Back(left - 1); Spot[ci][cj] = 0; HasR[ci] ^= (1 << val); HasC[cj] ^= (1 << val); HasS[Sq[ci][cj]] ^= (1 << val); } } } int main() { cin >> n; dim = n * n; Preprocess(); for(int i = 0; i < dim; ++i) for(int j = 0; j < dim; ++j) cin >> Spot[i][j]; int left = 0; for(int i = 0; i < dim; ++i) { for(int j = 0; j < dim; ++j) { if(Spot[i][j] > 0) { int what = Spot[i][j] - 1; HasR[i] |= (1 << what); HasC[j] |= (1 << what); HasS[Sq[i][j]] |= (1 << what); } else left += 1; } } // cerr << "left: " << left << endl; Back(left); assert(false); return 0; }