当前位置:网站首页>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. :


#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 :

Then there is the action , Write the answer into the question !!
There are still better ways , Welcome to the comment area to discuss ~~
边栏推荐
- Li Kou today's question -729 My schedule I
- Redis cluster deployment based on redis5
- 2345 file shredding, powerful file deletion tool, unbound pure extract version
- Is there a case where sqlcdc monitors multiple tables and then associates them to sink to another table? All operations in MySQL
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
- Reset nodejs of the system
- 会员积分营销系统操作的时候怎样提升消费者的积极性?
- Trends in DDoS Attacks
- RobotFramework入门(一)简要介绍及使用
- Method of changing object properties
猜你喜欢

一个复制也能玩出花来
![[Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation](/img/e4/890e84ab8326e029c4915163904d85.jpg)
[Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation

The third level of C language punch in
![[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning](/img/37/83953c24ec141667b304f9dac440f1.jpg)
[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning

Lecture 4 of Data Engineering Series: sample engineering of data centric AI

Force buckle 146 LRU cache
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17](/img/85/2635afeb2edeb0f308045edd1f3431.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8](/img/16/33f5623625ba817e6e022b5cb7ff5d.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8

主数据管理(MDM)的成熟度

ReferenceError: primordials is not defined错误解决
随机推荐
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
Patch NTP server at the beginning of DDoS counterattack
Redis cluster deployment based on redis5
大厂镜像库
[coppeliasim] 6-DOF path planning
RobotFramework入门(三)WebUI自动化之百度搜索
Y a - t - il des cas où sqlcdc surveille plusieurs tables et les associe à une autre? Tout fonctionne dans MySQL
Data preparation
Redis delete policy
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
inherited constructors
如何精准识别主数据?
[postgraduate entrance examination English] prepare for 2023, learn list5 words
2020.02.11
MySQL (IV) - transactions
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
Template_ Quick sort_ Double pointer
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.3 linear algebra_ Learning thinking and exercise answers
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
How to generate rich text online