|
|
|
|
@ -32,4 +32,175 @@ for(i=0;i<9;i++)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
数独实验第3题
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
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 i, j,x,y,m=0;
|
|
|
|
|
printf("The original Sudoku matrix:\n ");
|
|
|
|
|
for (i = 0; i < 9; i++)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0; j < 9; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%-2d", board0[i][j]);
|
|
|
|
|
if (j == 8)
|
|
|
|
|
{
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < 9; i++)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0; j < 9; j++)
|
|
|
|
|
{
|
|
|
|
|
if (board0[i][j] != 0)
|
|
|
|
|
{
|
|
|
|
|
x = i + 1;
|
|
|
|
|
y = j + 1;
|
|
|
|
|
for (; y < 9;y++)
|
|
|
|
|
{
|
|
|
|
|
if (board0[i][j] != board0[i][y])
|
|
|
|
|
{
|
|
|
|
|
m = m;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (; x < 9; x++)
|
|
|
|
|
{
|
|
|
|
|
if (board0[i][j] != board0[x][j])
|
|
|
|
|
{
|
|
|
|
|
m = m;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (m == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("True:Valid initial Sudoku matrix!\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
数独实验第4题(部分)
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#define N 3
|
|
|
|
|
#define M 9
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
int x[M][M]={{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}};
|
|
|
|
|
void Solve(int r, int c);
|
|
|
|
|
int checkedbox(int r);
|
|
|
|
|
int checked(int r, int c, int num);
|
|
|
|
|
int* getData(int r, int c,int *data);
|
|
|
|
|
void show(void);
|
|
|
|
|
void Solve(int r, int c)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
if (r == M) {
|
|
|
|
|
show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (c == 0 && (r == N || r == N*2)) {
|
|
|
|
|
if (checkedbox(r) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (c == M) {
|
|
|
|
|
Solve(r+1, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (x[r][c] == 0) {
|
|
|
|
|
for (i = 1; i <= M; i++) {
|
|
|
|
|
if (checked(r,c,i) == 1) {
|
|
|
|
|
x[r][c]=i;
|
|
|
|
|
Solve(r, c+1);
|
|
|
|
|
x[r][c]=0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
Solve(r, c+1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int checkedbox(int r) {
|
|
|
|
|
int i,j,c;
|
|
|
|
|
int *data = malloc(sizeof(int)*M);
|
|
|
|
|
for (c = 0; c < M; c+=N) {
|
|
|
|
|
data = getData(r,c,data);
|
|
|
|
|
for(i = 0; i < M-1 ; i++) {
|
|
|
|
|
for(j = i+1 ; j < M ; j++) {
|
|
|
|
|
if(data[i] == data[j]) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(data);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
int* getData(int r, int c,int *data){
|
|
|
|
|
int i,j,k = 0;
|
|
|
|
|
for (i = r-N; i < r ; i++) {
|
|
|
|
|
for(j = c; j < c+N ; j++){
|
|
|
|
|
data[k++] = x[i][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
int checked(int r, int c, int num) {
|
|
|
|
|
int j = 0,flag = 1;
|
|
|
|
|
for (j = 0; j < M; j++) {
|
|
|
|
|
if (x[j][c]==num || x[r][j]==num) {
|
|
|
|
|
flag = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return flag;
|
|
|
|
|
}
|
|
|
|
|
void show(void) {
|
|
|
|
|
int i,j;
|
|
|
|
|
for (i = 0; i < M; i++) {
|
|
|
|
|
for (j = 0; j < M; j++) {
|
|
|
|
|
printf("%d ",x[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
Solve(0,0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|