当前位置:网站首页>Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
2022-07-06 02:38:00 【Fish ball】
use C Linguistic solution 9×9 Sudoku ( Close test available )( Thought analysis )
Sudoku , I believe we are all familiar , Just like this. :
![](/img/19/600c19dc75df56249e35839070b32e.jpg)
![](/img/50/bd3ff46b829774c137d10052b48f14.jpg)
#include <stdio.h>
#include <stdbool.h>
#include <windows.h>
#pragma warning(disable:4996);
// Read the incomplete Sudoku matrix
void readMatrix(int array[9][9])
{
int row, // That's ok
col; // Column
for (row = 0; row < 9; row++)
{
for (col = 0; col < 9; col++)
scanf(" %1d", &array[row][col]); /**** Be careful %1d And there are spaces in front of ****/
getchar();
}
}
// Determine whether there are duplicate numbers on rows and columns ( Return without repetition true, Repeat return false)
bool RowAndColRepeat(int array[9][9], int row, int col, int num)
{
int i; // Count
// Determine whether there are duplicate numbers on the line
for (i = 0; i < 9; i++)
if (array[row][i] == num)
return false;
// Determine whether there are duplicate numbers on the column
for (i = 0; i < 9; i++)
if (array[i][col] == num)
return false;
return true;
}
// Determine the position Which is it? 3x3 squared paper for practicing calligraphy
int getRowOrCol(int num)
{
if (num < 3)
return 0;
else if (num < 6)
return 3;
else
return 6;
}
// Judge 3x3 Is there a duplicate number in the Jiugong grid ( Return without repetition true, Repeat return false)
bool BlockRepeat(int array[9][9], int row, int col, int num)
{
int getRowOrCol(int num); // obtain 3x3 The function prototype declaration of the starting position of the Jiugong lattice
int rowStart, // Line number
colStart, // Column number
i, j; // Count
rowStart = getRowOrCol(row);
colStart = getRowOrCol(col);
for (i = rowStart; i < rowStart + 3; i++)
for (j = colStart; j < colStart + 3; j++)
if (array[i][j] == num)
return false;
return true;
}
// Deep search of Sudoku
void calculation(int array[9][9], int row, int col)
{
int i, j; // Count // recursive , Set up exits : If the line number row Beyond array row subscript , Exit recursive output answer
if (row > 8)
{
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
printf("%d ", array[i][j]);
printf("\n");
}
return;
}
// If array[row][col] == 0 That is, the number of this position is not determined
if (array[row][col] == 0)
{
for (i = 1; i < 10; i++)
// from 1 to 9 Start counting for(i = 1;i < 10;i++)
// Trial method : Judge whether there are duplicate numbers on the row and column of this position , Then judge where you are 3x3 Is it repeated in the palace
// If 1 to 9 There is a suitable number in , Then the number of this position is this number , And start traversing the next location
if (RowAndColRepeat(array, row, col, i) && BlockRepeat(array, row, col, i))
{
array[row][col] = i;
calculation(array, row + (col + 1) / 9, (col + 1) % 9); // When the cells of each row have been traversed , Go to the next line
}
array[row][col] = 0; // If there is no or incorrect number , Is reset to 0
}
else
{
// If the number of this position is determined, skip this position , Traverse the next position
calculation(array, row + (col + 1) / 9, (col + 1) % 9); // When the cells of each row have been traversed , Go to the next line
}
}
int main()
{
int array[9][9];// Make a statement 9x9 Sudoku matrix
printf(" Please enter the title :\n");
readMatrix(array);// Read Sudoku topics
printf("\n The result of the calculation is :\n");
calculation(array, 0, 0);// Start deep search from the first cell
system("pause");// Pause window , Show results
return 0;
}
The effect is as follows :
![](/img/c8/0f93496f8fd66162ecc07bf2cd30c5.jpg)
Then there is the action , Write the answer into the question !!
There are still better ways , Welcome to the comment area to discuss ~~
边栏推荐
- 深度解析链动2+1模式,颠覆传统卖货思维?
- 07 单件(Singleton)模式
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
- Accident index statistics
- RobotFramework入门(一)简要介绍及使用
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
- Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
- "Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.5 automatic differentiation_ Learning thinking and exercise answers
- 零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
猜你喜欢
Shell script updates stored procedure to database
Initial understanding of pointer variables
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
零基础自学STM32-野火——GPIO复习篇——使用绝对地址操作GPIO
The third level of C language punch in
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
Force buckle 146 LRU cache
Advanced technology management - what is the physical, mental and mental strength of managers
[robot library] awesome robots Libraries
随机推荐
RobotFramework入门(一)简要介绍及使用
在GBase 8c数据库中使用自带工具检查健康状态时,需要注意什么?
Redis skip table
Day 50 - install vsftpd on ceontos6.8
QT release exe software and modify exe application icon
Dachang image library
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
DDoS attacks - are we really at war?
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
General process of machine learning training and parameter optimization (discussion)
RobotFramework入门(三)WebUI自动化之百度搜索
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
HDU_ p1237_ Simple calculator_ stack
HDU_p1237_简单计算器_stack
Blue Bridge Cup group B provincial preliminaries first question 2013 (Gauss Diary)
[coppeliasim] efficient conveyor belt
怎么检查GBase 8c数据库中的锁信息?
Template_ Quick sort_ Double pointer
Reset nodejs of the system
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition