Write a function that will solve a 9x9 Sudoku puzzle. The function will take one argument consisting of the 2D puzzle array, with the value 0 representing an unknown square.
The Sudokus tested against your function will be "easy" (i.e. determinable; there will be no need to assume and test possibilities on unknowns) and can be solved with a brute-force approach.
For Sudoku rules, see the Wikipedia article.
const int sudoku[9][9] = {
{0,0,0,0,0,0,6,0,0},
{0,0,0,0,0,7,0,0,1},
{2,0,8,0,0,0,0,4,0},
{6,0,0,0,0,0,0,0,0},
{0,5,1,4,0,9,0,0,0},
{0,0,0,5,0,0,0,2,0},
{0,0,4,0,0,0,0,5,0},
{0,0,7,1,9,6,0,0,0},
{0,8,0,0,0,0,3,0,0},
};
#include <stddef.h>
typedef int (*T)[9];
T solve (const T sudoku) {
/* Write your code here */
return NULL;
}
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
#include <criterion/criterion.h>
#include <stdbool.h>
typedef int (*T)[9];
extern T solve(const int (*)[9]);
const int a[9][9] = {
{0,0,0,0,0,0,6,0,0},
{0,0,0,0,0,7,0,0,1},
{2,0,8,0,0,0,0,4,0},
{6,0,0,0,0,0,0,0,0},
{0,5,1,4,0,9,0,0,0},
{0,0,0,5,0,0,0,2,0},
{0,0,4,0,0,0,0,5,0},
{0,0,7,1,9,6,0,0,0},
{0,8,0,0,0,0,3,0,0},
};
const int b[9][9] = {
{0,0,0,0,0,6,6,0,0},
{0,0,0,0,0,7,0,0,1},
{2,0,8,0,0,0,0,4,0},
{6,0,0,0,0,0,0,0,0},
{0,5,1,4,0,9,0,0,0},
{0,0,0,5,0,0,0,2,0},
{0,0,4,0,0,0,0,5,0},
{0,0,7,1,9,6,0,0,0},
{0,8,0,0,0,0,3,0,0},
};
bool check( int(*x)[9] );
Test(the_multiply_function, should_pass_all_the_tests_provided) {
cr_assert_eq(check(solve(a)), true);
cr_assert_eq(solve(b), NULL);
}
bool check( int(*x)[9] ) {
if(!x) return false;
for(int i=0;i<9;i++){
for(int j=0,k[9]={0};j<9;j++){
if(!x[i][j])return false;
if(k[x[i][j]-1])return false;
else k[x[i][j]-1] = 1;
}
for(int j=0,k[9]={0};j<9;j++)
if(k[x[j][i]-1])return false;
else k[x[j][i]-1] = 1;
}
for(int i=0;i<3;i++)for(int j=0;j<3;j++)
for(int m[9]={0},k=0;k<3;k++)for(int l=0;l<3;l++)
if(m[x[i*3+k][j*3+l]-1])return false;
else m[x[i*3+k][j*3+l]-1] = 1;
return true;
}
Converts the string to Brainfuck.
#include <stdio.h>
#include <string.h>
// #define DEBUG
char s[4096] = "";
int s2b(int argc, char *argv[]) {
register int i = 0,j;
register int k,m;
register int l,last = 0;
if (argc != 2) {
printf("Usage: %s [string]",*argv);
return 1;
}
while (argv[1][++i]!=0)
continue;
for (j=0; j<i; j++) {
register int t;
t = argv[1][j];
l = k = t-last;
last = t;
#ifdef DEBUG
printf("\n%d, %d, %d, %d\n",last,k,j,i);
#endif
if (k >= 0) {
k >>= 3; // k /= 8
l &= 7; //在更多的机器工作,求余
if (k == 1)
strcat(s,">++++++++");
else {
if (k != 0) {
strcat(s,"++++++++[>");
for (m=0; m<k; m++)
strcat(s,"+");
strcat(s,"<-]>");
} else {
strcat(s,">");
}
}
for (m=0; m<l; m++)
strcat(s,"+");
strcat(s,".");
} else {
k = -k >> 3; // k /= 8
l = -l & 7; //在更多的机器工作,求余
if (k == 1)
strcat(s,">--------");
else {
strcat(s,"++++++++[>");
for (m=0; m<k; m++)
strcat(s,"-");
strcat(s,"<-]>");
}
for (m=0; m<l; m++)
strcat(s,"-");
strcat(s,".");
}
#ifdef DEBUG
printf("\n%d, %d, %d\n",m,k,l);
#endif
strcat(s,"<");
}
return 0;
}
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
#include <criterion/criterion.h>
#include <string.h>
// replace with the actual method being tested
int s2b(int argc, char *argv[]);
extern char s[];
Test(the_multiply_function, should_pass_all_the_tests_provided) {
char *argv[] ={"","hello, world!"};
s2b(2,argv);
cr_assert_eq(strcmp(s,"++++++++[>+++++++++++++<-]>.<++++++++[><-]>---.<>+++++++.<>.<>+++.<++++++++[>--------<-]>---.<>------------.<++++++++[>++++++++++<-]>+++++++.<>--------.<>+++.<++++++++[><-]>------.<>--------.<++++++++[>--------<-]>---.<"), 0);
}