In many newspapers we may nd some puzzles to solve, one of those is Su Doku. Given a grid 9 9
with some of entries lled, the objective is to ll in the grid so that every row, every column, and every
3 3 box contains the digits 1 through 9.
source: http://www.sudoku.com
Input
Input contains several test cases separated by a blank line. Each of them contains an integer n such
that 1 n 3 and a grid n
2 n
2
with some of the entries lled with digits from 1 to n
2
(an entrie
not lled will have 0). In this case, the objective is to ll in the grid so that every row, every column,
and every n n box contains the digits 1 through n
2
.
Output
A solution for the problem. If exists more than one, you should give the lower one assuming a lexico-
graphic order. If there is no solution, you should print `NO SOLUTION'. For lexicographic comparison
you should consider lines in rst place. Print a blank line between test cases.
Sample Input
3
0 6 0 1 0 4 0 5 0
0 0 8 3 0 5 6 0 0
2 0 0 0 0 0 0 0 1
8 0 0 4 0 7 0 0 6
0 0 6 0 0 0 3 0 0
7 0 0 9 0 1 0 0 4
5 0 0 0 0 0 0 0 2
0 0 7 2 0 6 9 0 0
0 4 0 5 0 8 0 7 0
Sample Output
9 6 3 1 7 4 2 5 8
1 7 8 3 2 5 6 4 9
2 5 4 6 8 9 7 3 1
8 2 1 4 3 7 5 9 6
4 9 6 8 5 2 3 1 7
7 3 5 9 6 1 8 2 4
5 8 9 7 1 3 4 6 2
3 1 7 2 4 6 9 8 5
6 4 2 5 9 8 1 7 3
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
const int N = 9;
int vis[N][N];
int n;
int g[N][N];
set<int> s;
int cas = 0;
void init()
{
for (int i = 1; i <= n * n; i++) {
s.insert(i);
}
}
bool input()
{
if (scanf("%d", &n) != 1) return false;
//printf("n=%d\n", n);
int m = n * n;
s.clear();
init();
memset(vis, 0x00, sizeof(vis));
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &g[i][j]);
if (g[i][j]) {
vis[i][j] = g[i][j];
}
}
}
#if 0
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
printf(" %d", g[i][j]);
}
printf("\n");
}
#endif
return true;
}
bool dfs(int row, int col)
{
if (row == n * n) return true;
if (g[row][col]) {
col++;
if (col >= n * n) {
row++, col = 0;
}
return dfs(row, col);
} else {
set<int> tmp = s;
for (int i = 0; i < n * n; i++) {
if (i != col && vis[row][i]) {
tmp.erase(vis[row][i]);
}
}
for (int i = 0; i < n * n; i++) {
if (i != row && vis[i][col]) {
tmp.erase(vis[i][col]);
}
}
int x1 = row / n, y1 = col / n;
for (int i = x1 * n; i < (x1 + 1) * n; i++) {
for (int j = y1 * n; j < (y1 + 1) * n; j++) {
if (i != row && j != col && vis[i][j]) tmp.erase(vis[i][j]);
}
}
if (tmp.empty()) return false;
#if 0
cout << "row:" << row << " col:" << col << " tmp:";
copy(tmp.begin(), tmp.end(), ostream_iterator<int>(cout, " "));
cout << endl;
#endif
for (set<int>::iterator it = tmp.begin(); it != tmp.end(); it++) {
g[row][col] = *it;
vis[row][col] = *it;
if (dfs(row, col)) return true;
g[row][col] = 0;
vis[row][col] = 0;
}
return false;
}
}
void solve()
{
if (cas++) printf("\n");
if (dfs(0, 0)) {
for (int i = 0; i < n * n; i++) {
for (int j = 0; j < n * n; j++) {
if (j) printf(" ");
printf("%d", g[i][j]);
}
printf("\n");
}
} else {
printf("NO SOLUTION\n");
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("e:\\uva_in.txt", "r", stdin);
#endif
while (input()) {
solve();
}
return 0;
}