Compare commits

..

2 Commits
main ... main

1
.gitignore vendored

@ -0,0 +1 @@
*.*~

28
.vscode/tasks.json vendored

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "C:\\Users\\GaoShiBo\\software\\mingw64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}

31
1.cpp

@ -1,31 +0,0 @@
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void matrixOutput(int matrixArr[9][9])
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
printf("%d", matrixArr[i][j]);
if (j != 8)
printf(" ");
}
if (i != 8)
printf("\n");
}
}
int main()
{
int board[9][9] = {{5, 3, 4, 6, 7, 8, 9, 1, 2},
{6, 7, 2, 1, 9, 5, 3, 4, 8},
{1, 9, 8, 3, 4, 2, 5, 6, 7},
{8, 5, 9, 7, 6, 1, 4, 2, 3},
{4, 2, 6, 8, 5, 3, 7, 9, 1},
{7, 1, 3, 9, 2, 4, 8, 5, 6},
{9, 6, 1, 5, 3, 7, 2, 8, 4},
{2, 8, 7, 4, 1, 9, 6, 3, 5},
{3, 4, 5, 2, 8, 6, 1, 7, 9}};
matrixOutput(board);
return 0;}

82
3.cpp

@ -1,82 +0,0 @@
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int checkMatrix(int matrixArr[9][9], int isPrintInfo){
int rowCnt[9][10]={0};
int colCnt[9][10]={0};
int bloCnt[9][10]={0};
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(matrixArr[i][j]!=0){
rowCnt[i][matrixArr[i][j]]++;
if(rowCnt[i][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!");
printf("\nThe number %d in the row %d has been used!",matrixArr[i][j],i+1);
}
return 0;
}
colCnt[j][matrixArr[i][j]]++;
if(colCnt[j][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!");
printf("\nThe number %d in the col %d has been used!",matrixArr[i][j],j+1);
}
return 0;
}
bloCnt[i/3*3+j/3][matrixArr[i][j]]++;
if(bloCnt[i/3*3+j/3][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!");
printf("\nThe number %d in the block %d has been used!",matrixArr[i][j],i/3*3+j/3+1);
}
return 0;
}
}
}
}
if(isPrintInfo){
printf("\nTrue:Valid initial Sudoku matrix!");
}
return 1;
}
void matrixOutput(int matrixArr[9][9])
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
printf("%d", matrixArr[i][j]);
if (j != 8)
printf(" ");
}
if (i != 8)
printf("\n");
}
}
int main(){
int board0[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
int board1[9][9] = {{8, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printf("The original Sudoku matrix: \n");
matrixOutput(board1);
checkMatrix(board1,1);
return 0;
}

@ -0,0 +1,2 @@
# matrix

BIN
a.out

Binary file not shown.

@ -0,0 +1,67 @@
#include "myMatrix.h"
int checkMatrix(int matrixArr[9][9], int isPrintInfo){
int rowCnt[9][10]={0};
int colCnt[9][10]={0};
int bloCnt[9][10]={0};
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(matrixArr[i][j]!=0){
rowCnt[i][matrixArr[i][j]]++;
if(rowCnt[i][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!\n");
printf("\nThe number %d in the row %d has been used!\n",matrixArr[i][j],i+1);
}
return 0;
}
colCnt[j][matrixArr[i][j]]++;
if(colCnt[j][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!\n");
printf("\nThe number %d in the col %d has been used!\n",matrixArr[i][j],j+1);
}
return 0;
}
bloCnt[i/3*3+j/3][matrixArr[i][j]]++;
if(bloCnt[i/3*3+j/3][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!\n");
printf("\nThe number %d in the block %d has been used!\n",matrixArr[i][j],i/3*3+j/3+1);
}
return 0;
}
}
}
}
if(isPrintInfo){
printf("\nTrue:Valid initial Sudoku matrix!\n");
}
return 1;
}
// int main(){
// int board0[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
// {6, 0, 0, 1, 9, 5, 0, 0, 0},
// {0, 9, 8, 0, 0, 0, 0, 6, 0},
// {8, 0, 0, 0, 6, 0, 0, 0, 3},
// {4, 0, 0, 8, 0, 3, 0, 0, 1},
// {7, 0, 0, 0, 2, 0, 0, 0, 6},
// {0, 6, 0, 0, 0, 0, 2, 8, 0},
// {0, 0, 0, 4, 1, 9, 0, 0, 5},
// {0, 0, 0, 0, 8, 0, 0, 7, 9}};
// int board1[9][9] = {{8, 3, 0, 0, 7, 0, 0, 0, 0},
// {6, 0, 0, 1, 9, 5, 0, 0, 0},
// {0, 9, 8, 0, 0, 0, 0, 6, 0},
// {8, 0, 0, 0, 6, 0, 0, 0, 3},
// {4, 0, 0, 8, 0, 3, 0, 0, 1},
// {7, 0, 0, 0, 2, 0, 0, 0, 6},
// {0, 6, 0, 0, 0, 0, 2, 8, 0},
// {0, 0, 0, 4, 1, 9, 0, 0, 5},
// {0, 0, 0, 0, 8, 0, 0, 7, 9}};
//
// printf("The original Sudoku matrix: \n");
// matrixOutput(board1);
// checkMatrix(board1,1);
// return 0;
// }

@ -1,130 +1,75 @@
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int checkMatrix(int matrixArr[9][9], int isPrintInfo){
int rowCnt[9][10]={0};
int colCnt[9][10]={0};
int bloCnt[9][10]={0};
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(matrixArr[i][j]!=0){
rowCnt[i][matrixArr[i][j]]++;
if(rowCnt[i][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!");
printf("\nThe number %d in the row %d has been used!",matrixArr[i][j],i+1);
}
return 0;
}
colCnt[j][matrixArr[i][j]]++;
if(colCnt[j][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!");
printf("\nThe number %d in the col %d has been used!",matrixArr[i][j],j+1);
}
return 0;
}
bloCnt[i/3*3+j/3][matrixArr[i][j]]++;
if(bloCnt[i/3*3+j/3][matrixArr[i][j]]>1){
if(isPrintInfo){
printf("\nFalse:Invalid initial Sudoku matrix!");
printf("\nThe number %d in the block %d has been used!",matrixArr[i][j],i/3*3+j/3+1);
}
return 0;
}
}
}
}
if(isPrintInfo){
printf("\nTrue:Valid initial Sudoku matrix!");
}
return 1;
}
void matrixOutput(int matrixArr[9][9])
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
printf("%d", matrixArr[i][j]);
if (j != 8)
printf(" ");
}
if (i != 8)
printf("\n");
}
}
int fillMatrix(int matrixArr[9][9], int row, int col){
if(row==9){
return 1;
}
if(col==9){
return fillMatrix(matrixArr,row+1,0);
}
if(matrixArr[row][col]!=0){
return fillMatrix(matrixArr, row, col+1);
}
for(int i=1;i<=9;i++){
matrixArr[row][col]=i;
if(checkMatrix(matrixArr,0)){
if(fillMatrix(matrixArr,row,col+1)){
return 1;
}
matrixArr[row][col]=0;
}else{
matrixArr[row][col]=0;
}
}
return 0;
}
int main()
{
int board0[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
int board1[9][9] = {{8, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
int board2[9][9] = {{5, 2, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printf("The original Sudoku matrix: \n");
matrixOutput(board0);
if (checkMatrix(board0, 1))
{
if (fillMatrix(board0, 0, 0))
{
printf("\nThe solution of Sudoku matrix:\n");
matrixOutput(board0);
}
else
{
printf("\nNo solution!");
}
}
else
{
printf("\nNo solution!");
}
return 0;
}
#include "myMatrix.h"
int fillMatrix(int matrixArr[9][9], int row, int col){
if(row==9){
return 1;
}
if(col==9){
return fillMatrix(matrixArr,row+1,0);
}
if(matrixArr[row][col]!=0){
return fillMatrix(matrixArr, row, col+1);
}
for(int i=1;i<=9;i++){
matrixArr[row][col]=i;
if(checkMatrix(matrixArr,0)){
if(fillMatrix(matrixArr,row,col+1)){
return 1;
}
matrixArr[row][col]=0;
}else{
matrixArr[row][col]=0;
}
}
return 0;
}
int main()
{
int board0[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
int board1[9][9] = {{8, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
int board2[9][9] = {{5, 2, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printf("The original Sudoku matrix: \n");
matrixOutput(board0);
if (checkMatrix(board0, 1))
{
if (fillMatrix(board0, 0, 0))
{
printf("\nThe solution of Sudoku matrix:\n");
matrixOutput(board0);
}
else
{
printf("\nNo solution!");
}
}
else
{
printf("\nNo solution!");
}
return 0;
}

@ -0,0 +1,7 @@
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void matrixOutput(int matrixArr[9][9]);
void matrixRandomInit(int matrixArr[9][9]);
int checkMatrix(int matrixArr[9][9], int isPrintInfo);

@ -0,0 +1,38 @@
#include "myMatrix.h"
void matrixOutput(int matrixArr[9][9])
{
for (int i = 0; i < 9; i++)
{
if(i % 3 == 0)
printf("|-----------------------|\n");
for (int j = 0; j < 9; j++)
{
if(j % 3 == 0){
printf("| ");
}
printf("%d", matrixArr[i][j]);
if (j != 8)
printf(" ");
else
printf(" |");
}
printf("\n");
}
printf("|-----------------------|\n");
}
// int main()
// {
// int board[9][9] = {{5, 3, 4, 6, 7, 8, 9, 1, 2},
// {6, 7, 2, 1, 9, 5, 3, 4, 8},
// {1, 9, 8, 3, 4, 2, 5, 6, 7},
// {8, 5, 9, 7, 6, 1, 4, 2, 3},
// {4, 2, 6, 8, 5, 3, 7, 9, 1},
// {7, 1, 3, 9, 2, 4, 8, 5, 6},
// {9, 6, 1, 5, 3, 7, 2, 8, 4},
// {2, 8, 7, 4, 1, 9, 6, 3, 5},
// {3, 4, 5, 2, 8, 6, 1, 7, 9}};
// matrixOutput(board);
// return 0;
// }

@ -1,55 +1,38 @@
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void matrixOutput(int matrixArr[9][9])
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
printf("%d", matrixArr[i][j]);
if (j != 8)
printf(" ");
}
if (i != 8)
printf("\n");
}
}
void matrixRandomInit(int matrixArr[9][9]){
int map[9] = {1,2,3,4,5,6,7,8,9};
int mp;
srand(time(NULL));
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
matrixArr[i][j] = 0;
}
}
for(int i=0;i<9;i++){
if(i%3==0){
mp=0;
for(int j=0;j<8;j++){
int n = i+rand()%(8-i);
int tmp = map[i];
map[i] = map[n];
map[n]=tmp;
}
}
int k=0;
while(k<3){
int tmpP = rand()%9;
if(matrixArr[i][tmpP]==0){
matrixArr[i][tmpP] = map[mp++];
k++;
}
}
}
}
int main(){
int board[9][9];
matrixRandomInit(board);
matrixOutput(board);
return 0;
}
#include "myMatrix.h"
void matrixRandomInit(int matrixArr[9][9]){
int map[9] = {1,2,3,4,5,6,7,8,9};
int mp;
srand(time(NULL));
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
matrixArr[i][j] = 0;
}
}
for(int i=0;i<9;i++){
if(i%3==0){
mp=0;
for(int j=0;j<8;j++){
int n = i+rand()%(8-i);
int tmp = map[i];
map[i] = map[n];
map[n]=tmp;
}
}
int k=0;
while(k<3){
int tmpP = rand()%9;
if(matrixArr[i][tmpP]==0){
matrixArr[i][tmpP] = map[mp++];
k++;
}
}
}
}
int main(){
int board[9][9];
matrixRandomInit(board);
matrixOutput(board);
return 0;
}

Binary file not shown.
Loading…
Cancel
Save