Sudoku is a traditional puzzle game, you need to complete a 9 × 9 Sudoku complete, so that each row, column, and each 3 × 3 number in the nine-square grid in the figure appear exactly once.
Please write a program to fill in Sudoku.
Sudoku.png
Input format
The input contains multiple sets of test cases.
Each test case occupies one line and contains 81 characters, representing the data within 81 cells of Sudoku (the overall order is from top to bottom, and the peers are from left to right).
Each character is a number (1-9) or a "." (Indicating that it has not been filled).
You can assume that there is only one solution for each puzzle in the input.
At the end of the file is a single line containing the word "end", indicating the end of input.
Output format
For each test case, a line of data is output, which represents the filled Sudoku.
Sample input:
.2738…1…1…6735…293.5692.8…6.1745.364…9518…7…8…6534.
…52…8.4…3…9…5.1…6…2…7…3…6…1…7.4…3.
end
Sample output:
527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936
Ideas: I didn't expect to write, the lyd code was transported at the nano level
The idea of direct explosive search has to reach the complexity of tens of tens of 9, and we adjust the way of searching.
First of all, the number on each grid corresponds to 3 parts-horizontal, vertical, 9 small. Each part 1 ~ 9 is not repeated. This can start from the direction of reducing repetition. Because there are only nine, each with and without states, you can think of using binary to determine whether to repeat. For each part, use x, y, and z arrays to record the status.
When searching, traverse each grid, and by calculating the sum value of the grid for the three parts, you can get how many numbers are determined by the three parts, and get the undetermined part with the most determined numbers. Fill in this part.
In the middle, the lowbit operation is used to get the position of the last 1 each time.
#include <cstdio>
#include <cstring>
using namespace std;
char s[105];
int gx,gy,gz;
int cnt[1005],f[1005],X[105],Y[105],Z[105];
int x[105],y[105],z[105];
void get(int i)
{
gx = X[i];
gy = Y[i];
gz = Z[i];
}
void work(int i,int k)
{
get(i);
x[gx] ^= (1 << k);
y[gy] ^= (1 << k);
z[gz] ^= (1 << k);
}
bool dfs(int ans)
{
if(!ans)return true;
int k = 10,t,w;
for(int i = 0;i < 81;i++)
{
if(s[i] == '.')
{
get(i);
w = x[gx] & y[gy] & z[gz];
if(cnt[w] < k)
{
k = cnt[w];
t = i;
}
}
}
get(t);
w = x[gx] & y[gy] & z[gz];
while(w)
{
int now = f[w & -w];
s[t] = now + '1';
work(t,now);
if(dfs(ans - 1))return true;
work(t,now);
w -= w & (-w);
s[t] = '.';
}
return false;
}
void get_cnt(int i)
{
int num = 0;
int tmp = i;
while(tmp)
{
num++;
tmp -= tmp & (-tmp);
}
cnt[i] = num;
}
void solve()
{
for(int i = 0;i < 9;i++)x[i] = (1 << 9) - 1,y[i] = (1 << 9) - 1,z[i] = (1 << 9) - 1;
int ans = 0;
for(int i = 0;i < 81;i++)
{
if(s[i] != '.')work(i,s[i] - '1');
else ans++;
}
if(dfs(ans))
{
for(int i = 0;i < 81;i++)printf("%c",s[i]);
}
printf("\n");
}
int main()
{
for(int i = 0;i < (1 << 9);i++)get_cnt(i);
for(int i = 0;i < 9;i++)f[1 << i] = i;
for(int i = 0;i < 81;i++)
{
X[i] = i / 9;
Y[i] = i % 9;
Z[i] = X[i] / 3 * 3 + Y[i] / 3;
}
while(~scanf("%s",s))
{
if(s[0] == 'e')break;
solve();
}
return 0;
}
Read "On the Informatics Contest" 0 "and" 1 "," paper, I made this question, feel benefit a paper written very easy to understand Topic Link to a POJ2676:http://poj.org/p...
Title link:http://poj.org/problem?id=3074 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10451 Accepted: 3776 Description In the game of Sudoku, yo...
&nbs...
...
T1 T2 T3 These three questions seem to be very similar, it is more experience than QAQ. Ok, it’s totally different QAQ First look at T1, this is naked Sudoku, QAQ, direct violence I have passed ...
Sudoku is a traditional puzzle game, you need to put a number of 9 × 9 full complement alone, so that the figures each row, column, each of the 3 × 3 digital Jiugongge the average 1 to 9 a...
Title Description Sudoku is a use of paper, pen calculation logic game. The known digital players need to 9 × 9 on disk, all numbers inferred remaining spaces, and meet each row, each column, ea...
Sudoku topic: You must have heard of the "Sudoku" game. As shown in the figure below, the player needs to infer all the numbers with the remaining spaces based on the known numbers on the 9&...
Sudoku is a logic game that uses paper and pen to perform calculations. Players need to infer all the numbers in the remaining spaces based on the known numbers on the 9×9 board, and...
Problem Description You must have heard of the "Sudoku" game. As shown in the figure below, the player needs to infer all the numbers with the remaining spaces based on the known numbers on ...