#include <bits/stdc++.h> using i64 = long long; struct node { int x,y,row,col,grid,vis[3]; node():x(0),y(0),row(0),col(0),grid(0){ memset(vis,0,sizeof(vis)); } }; clock_t start,finish; int c[10][10]; struct node d[10][10]; void output(){ finish = clock(); std::cout << std::fixed << std::setprecision(3) << (double)(finish - start) / (1000.0) << " s\n"; for(int i = 1; i <= 9; i++){ for(int j = 1; j <= 9; j++){ std::cout << c[i][j] << " \n"[j == 9]; } } } void updateRow(int x, int digit){ int mask = (1 << digit); for(int i = 1; i <= 9; i++){ d[x][i].row |= mask; } } void updateCol(int y, int digit){ int mask = (1 << digit); for(int i = 1; i <= 9; i++){ d[i][y].col |= mask; } } void updateGrid(int x, int y, int digit){ int mask = (1 << digit); int gx = (x - 1) / 3 * 3 + 1; int gy = (y - 1) / 3 * 3 + 1; for(int i = gx; i < gx + 3; i++){ for(int j = gy; j < gy + 3; j++){ d[i][j].grid |= mask; } } } void reduceRow(int x, int digit){ int mask = (1 << digit) ; for(int i = 1; i <= 9; i++){ d[x][i].row ^= mask; } } void reduceCol(int y, int digit){ int mask = (1 << digit); for(int i = 1; i <= 9; i++){ d[i][y].col ^= mask; } } void reduceGrid(int x, int y, int digit){ int mask = (1 << digit); int gx = (x - 1) / 3 * 3 + 1; int gy = (y - 1) / 3 * 3 + 1; for(int i = gx; i < gx + 3; i++){ for(int j = gy; j < gy + 3; j++){ d[i][j].grid ^= mask; } } } void dfs(int x,int y){ //termination int cnt = 0; for(int i = 1; i <= 9; i++){ cnt += (__builtin_popcount(d[i][i].row) == 9 && __builtin_popcount(d[i][i].col) == 9); } if(cnt == 9){ output(); exit(0); } //TODO int dx = 1, dy = 1, max = -(1<<30); for(int i = 1; i <= 9; i++){ for(int j = 1; j <= 9; j++){ if(c[i][j]) continue; int rowp = __builtin_popcount(d[i][j].row); int colp = __builtin_popcount(d[i][j].col); int gridp = __builtin_popcount(d[i][j].grid); if(max < rowp + colp + gridp){ max = rowp + colp + gridp; dx = i, dy = j; } } } //enum for(int digit = 1; digit <= 9; digit++){ int mask = (1 << digit); if( d[dx][dy].row & mask || d[dx][dy].col & mask || d[dx][dy].grid & mask){ continue; } updateRow(dx, digit); updateCol(dy, digit); updateGrid(dx, dy, digit); c[dx][dy] = digit; dfs(dx, dy); c[dx][dy] = 0; reduceRow(dx, digit); reduceCol(dy, digit); reduceGrid(dx, dy, digit); } } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cerr.tie(nullptr); for(int i = 1; i <= 9; i++){ for(int j = 1; j <= 9; j++){ std::cin >> c[i][j]; if(c[i][j]) { updateRow(i, c[i][j]); updateCol(j, c[i][j]); updateGrid(i, j, c[i][j]); } } } start = clock(); dfs(1, 1); return 0; }