tags: C language
time limit:1000ms Memory limit:10000K Total time limit:3000ms
description:
Sudoku game rules
In the 9th-order square matrix, there are 81 small grids (nine columns and nine rows), which are divided into nine small squares (called palaces), each of which has nine small grids .
At the beginning of the game, some small cells on the board have already been filled with numbers (called the first game), and the player should fill in the numbers from 1 to 9 in the blank cells.
makes no repeated numbers appear in each row, column, and house at the end, and each game has only one unique solution (called the final game).
enter:
A 9*9 matrix, 0 means that the position is blank.
Output:
A 9*9 matrix, the format is similar to the input.
Input sample:
900050060
020070100
300102040
703800529
000345000
516009403
050208006
007090010
030010004
Sample output:
971453268
428976135
365182947
743861529
892345671
516729483
154238796
687594312
239617854
#include<iostream>
using namespace std;
void search(int m);
int check(int m);
void output();
int a[9][9];
char t[9][9];
int main()
{
int i,j;
for(i=0;i<9;i++)//Enter Sudoku
{
for(j=0;j<9;j++)
{
cin>>t[i][j];
}
}
for(i=0;i<9;i++)//Convert to plastic
{
for(j=0;j<9;j++)
{
a[i][j]=t[i][j]-'0';
}
}
search(0);
return 0;
}
void search(int m)
{
if(m==81)//Satisfy the conditions, output
{
output();
}
else
{
if(a[m/9][m%9]==0)//This box is empty, start testing
{
for(int i=1;i<=9;i++)//Test one by one
{
a[m/9][m%9]=i;
if(check(m))//Satisfy the check conditions, search for the next grid
{
search(m+1);
a[m/9][m%9]=0;//If you don’t keep searching, return to continue testing the current grid
}
else//Do not meet the conditions, continue to test
{
a[m/9][m%9]=0;
}
}
}
else//This cell is not empty, continue searching
{
search(m+1);
}
}
}
int check(int m)//Detect
{
int i,j;
int up,down,left,right;
up=m/9/3*3;//Frame the palace of the current search grid
down=up+3;
left=m%9/3*3;
right=left+3;
for(i=0;i<9;i++)//Detect horizontal and vertical lines
{
if((a[i][m%9]==a[m/9][m%9]&&i!=m/9)||(a[m/9][i]==a[m/9][m%9]&&i!=m%9))
{
return 0;
}
}
for(i=up;i<down;i++)//The house of the test
{
for(j=left;j<right;j++)
{
if(a[i][j]==a[m/9][m%9]&&i!=m/9&&j!=m%9)
{
return 0;
}
}
}
return 1;
}
void output()//Output result
{
for(int i=0;i<9;i++)
{
for(int j=0;j<8;j++)
{
cout<<a[i][j];
}
cout<<a[i][8]<<endl;
}
}
Write a solver for a Sudoku game The code is as follows...
Using Matlab GUI to realize Sudoku game, because it is still a novice in matlab, only the simplest functions can be realized at present. Step 1: Enter guide on the command line and design the GUI inte...
Problem Description: The Jiugong grid is in 81 grids (9×9), and the following conditions must be met: (1) The 9 grids in each horizontal row and vertical column contain numbers 1-9, and they are...
dfs practice questions This example of Sudoku needs to be operated internally in each recursive call, and has a deeper understanding of the operation principle of dfs...
Title description Inkara, a Finnish mathematician, spent 3 months designing the most difficult Sudoku game in the world, and it has only one answer. Inkara said that only the fastest thinking and smar...
Welcome to try ZJYYCOJ Article first published on topic Finnish mathematician Inkara took three months to design the most difficult Sudoku game in the world so far, and it has only one answer. Inkara ...
Title description Inkara, a Finnish mathematician, spent 3 months designing the most difficult Sudoku game in the world, and it has only one answer. Inkara said onlyBold styleThe person with the faste...
You must have heard of the "Sudoku" game. such as [Picture 1.png], the player needs to infer all the numbers of the remaining spaces based on the known numbers on the 9×9 disk, and sat...
C++ Sudoku game Directly on the code: Test example results Baidu Encyclopedia The World's Hardest Sudoku The answer comes out in seconds, there is nothing that cannot be solved by violence~ Synchronou...
The 6th question of the Lanqiao Cup intramural selection contest, the original question is as follows: You must have heard of the "Sudoku" game. As shown in the figure below, the player need...