You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pa37f2lwq 5a2017020d
Update README.md
2 years ago
README.md Update README.md 2 years ago

README.md

声明:本次实验中自行定义了头文件: Rand_init.h #pragma once

#ifndef RAND_INIT_H #define RAND_INIT_H // !RAND_INIT_H

void Rand_init(int in[][10]); #endif

STDotp.h

#ifndef STDOTP_H #define STDOTP_H

void OutPut(int metric[][10]); // Function declaration

#endif

check.h

#pragma once #ifndef CHECK_H_ #define CHECK_H_ bool check(int A[][10]); #endif // !CHECK_H

1、.9*9数组格式化输出

【源程序】 #include<stdio.h>

void Output(int metric[][10]) { for (int i = 1; i <= 3; i++) { printf("\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i - 2][3 * j - 2], metric[3 * i - 2][3 * j - 1], metric[3 * i - 2][3 * j]); } printf("|\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i - 1][3 * j - 2], metric[3 * i - 1][3 * j - 1], metric[3 * i - 1][3 * j]); } printf("|\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i][3 * j - 2], metric[3 * i][3 * j - 1], metric[3 * i][3 * j]); } printf("|\n"); } printf("\n"); }

int main() { int metric[10][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, {91, 92, 93, 94, 95, 96, 97, 98, 99, 100}};

Output(metric);

return 0; }

【运行结果】

【源程序】 #include<stdio.h> #include<stdlib.h> #include <time.h>

void Rand_init(int in[][10]) { srand(time(NULL)); for (int i = 1; i <= 9; i++) { int nloc[10] = { 0 }; for (int j = 1; j <= 3; j++) { int n = rand() % 10; int loc = rand() % 10; while (in[i][loc] != 0 || nloc[n] == 1 || n == 0 || loc == 0) { loc = rand() % 10; n = rand() % 10; } in[i][loc] = n; nloc[n] = 1; } } }

void Output(int metric[][10]) { for (int i = 1; i <= 3; i++) { printf("\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i - 2][3 * j - 2], metric[3 * i - 2][3 * j - 1], metric[3 * i - 2][3 * j]); } printf("|\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i - 1][3 * j - 2], metric[3 * i - 1][3 * j - 1], metric[3 * i - 1][3 * j]); } printf("|\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i][3 * j - 2], metric[3 * i][3 * j - 1], metric[3 * i][3 * j]); } printf("|\n"); } printf("\n"); }

int main() { int metric[10][10] = { 0 };

Rand_init(metric); Output(metric);

return 0; }

【运行结果】

【源程序】 #include"Rand_init.h" #include"STDotp.h" #include<stdio.h> #include<stdlib.h> #include <time.h>

void check(int A[][10]) { int row[10][10] = { 0 }; int col[10][10] = { 0 }; int block[4][4][10] = { 0 }; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (A[i][j] != 0) { int k = A[i][j]; if (row[i][k] != 0 || col[j][k] != 0 || block[(i + 2) / 3][(j + 2) / 3][k] != 0) { printf("%d %d %d", i, j, k); if (row[i][k] != 0) { printf("\nrow\n"); } if (col[j][k] != 0) { printf("\ncol\n"); } if (block[(i + 2) / 3][(j + 2) / 3][k] != 0) { printf("\nblock\n"); } printf("false"); return; } else { row[i][k]++; col[j][k]++; block[i / 3][j / 3][k]++; } } } } }

void Rand_init(int in[][10]) { srand(time(NULL)); for (int i = 1; i <= 9; i++) { int nloc[10] = { 0 }; for (int j = 1; j <= 3; j++) { int n = rand() % 10; int loc = rand() % 10; while (in[i][loc] != 0 || nloc[n] == 1 || n == 0 || loc == 0) { loc = rand() % 10; n = rand() % 10; } in[i][loc] = n; nloc[n] = 1; } } }

void Output(int metric[][10]) { for (int i = 1; i <= 3; i++) { printf("\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i - 2][3 * j - 2], metric[3 * i - 2][3 * j - 1], metric[3 * i - 2][3 * j]); } printf("|\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i - 1][3 * j - 2], metric[3 * i - 1][3 * j - 1], metric[3 * i - 1][3 * j]); } printf("|\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i][3 * j - 2], metric[3 * i][3 * j - 1], metric[3 * i][3 * j]); } printf("|\n"); } printf("\n"); }

int main() { int A[10][10] = { 0 }; Rand_init(A); Output(A); check(A); return 0; }

【运行结果】

【源程序】 #include<stdio.h> #include<stdbool.h> #include"check.h" #include"STDotp.h"

bool isSafe(int A[][10], int row, int col, int num) { // 检查行和列是否有重复的数字 for (int i = 1; i < 10; ++i) { if (A[row][i] == num || A[i][col] == num) { return false; } }

// 检查3x3子网格是否有重复的数字
int startRow = row - (row-1) % 3;
int startCol = col - (col-1) % 3;
for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
        if (A[startRow + i][startCol + j] == num) {
            return false;
        }
    }
}

return true;

}

bool solveSudoku(int A[][10]) { for (int row = 1; row < 10; ++row) { for (int col = 1; col < 10; ++col) { if (A[row][col] == 0) { // 0 表示未填充的位置 for (int num = 1; num <= 9; ++num) { if (isSafe(A,row,col,num)) { A[row][col] = num; if (solveSudoku(A)) { return true; } A[row][col] = 0; } } // 回溯,撤销之前 return false; // 无法填充有效数字 } } } return true; // 所有位置都已填充 }

void Output(int metric[][10]) { for (int i = 1; i <= 3; i++) { printf("\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i - 2][3 * j - 2], metric[3 * i - 2][3 * j - 1], metric[3 * i - 2][3 * j]); } printf("|\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i - 1][3 * j - 2], metric[3 * i - 1][3 * j - 1], metric[3 * i - 1][3 * j]); } printf("|\n"); for (int j = 1; j <= 3; j++) { printf("|%d %d %d", metric[3 * i][3 * j - 2], metric[3 * i][3 * j - 1], metric[3 * i][3 * j]); } printf("|\n"); } printf("\n"); }

int main() { int A[10][10] = { {0,0,0,0,0,0,0,0,0,0}, {0,5, 3, 0, 0, 7, 0, 0, 0, 0}, {0,6, 0, 0, 1, 9, 5, 0, 0, 0}, {0,0, 9, 8, 0, 0, 0, 0, 6, 0}, {0,8, 0, 0, 0, 6, 0, 0, 0, 3}, {0,4, 0, 0, 8, 0, 3, 0, 0, 1}, {0,7, 0, 0, 0, 2, 0, 0, 0, 6}, {0,0, 6, 0, 0, 0, 0, 2, 8, 0}, {0,0, 0, 0, 4, 1, 9, 0, 0, 5}, {0,0, 0, 0, 0, 8, 0, 0, 7, 9} };

if (solveSudoku(A)) {
    printf("解决的数独\n");
    Output(A);
}
else {
    printf("无解的数独\n");
    Output(A);
}

return 0;

}

【运行结果】