题目:
Code:
#include <bits/stdc++.h>
//#ifndef ONLINE_JUDGE
//#pragma GCC optimize(2)
//#endif // ONLINE_JUDGE
using namespace std;
class Shudu
{
bool nodes[10][10][10]; // Current block (i, j) can be filled by k
bool row[10][10], col[10][10], area[10][10]; // Sum of numbers on a row/col/area
int sum; // Sum of solved blocks
public:
int solved[10][10]; // Current status of table
Shudu()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
int x;
cin >> x;
for (int k = 1; k <= 9; k++) nodes[i][j][k] = !(bool)x;
solved[i][j] = x;
nodes[i][j][x] = (bool)x;
row[i][x] = col[j][x] = (bool)x;
sum += (bool)x;
}
}
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
int x = 3 * i - 2;
int y = 3 * j - 2;
int p = 3 * i - 3 + j;
for (int k = x; k <= x + 2; k++)
{
for (int l = y; l <= y + 2; l++)
{
area[p][solved[k][l]] = (bool)solved[k][l];
}
}
}
}
}
bool solve(int x, int y)
{
cerr << x << " " << y << endl;
int cnt = 0; // Sum of solves
int slv = 0; // One of solve
for (int i = 1; i <= 9; i++)
{
int p = x / 3.1 + y / 3.1;
nodes[x][y][i] = !(row[x][i] || col[y][i] || area[p][i]);
cnt += nodes[x][y][i];
if (nodes[x][y][i]) slv = i;
}
if (cnt)
{
if (cnt == 1)
{
solved[x][y] = slv;
sum++;
return 0;
}
}
else
{
exit(1);
}
return 0;
}
bool judge()
{
return sum < 81;
}
} shudu;
int main()
{
while(shudu.judge())
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
if (!shudu.solved[i][j]) shudu.solve(i, j);
}
}
}
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
cout << shudu.solved[i][j] << " ";
}
cout << endl;
}
return 0;
}