# Need some help for a sudoku solver ...

Discussion in 'C' started by axilleask, Nov 25, 2007.

Joined:
Nov 11, 2007
Messages:
2
0
Trophy Points:
0
Hi all

I have made this program that meant to read sudoku puzzles from file, solve them and then write the solution to file.

My problem is that when I try to write the solved puzzle on file no file is created.
I am stuck at this point..

My code is below..
Code:
```/* Sudoku Solver

*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* S=3 for standard 9x9 sudoku, max. S=4  */
#define S 2
#define N (S*S)

/*  loop with last not included */
#define loop(_I,_From,_To) for (_I=_From;_I<_To;_I++)

/* loop with last included */
#define loopto(_I,_From,_To) for (_I=_From;_I<=_To;_I++)

#define Error(X) do { fprintf(stderr,"ERROR %s\n",X); exit(1); } while(0)

typedef signed char sudoku_t[N][N];

int valid(int x,int y,int k,sudoku_t s)
/* returns 1 if char k can be put to s[x][y] */
{
int i,j,x0,y0;

loop (i,0,N) if (s[i][y]==k) return 0;
loop (j,0,N) if (s[x][j]==k) return 0;

x0=S*(x/S);
y0=S*(y/S);
loop (i,x0,x0+S)
loop (j,y0,y0+S) if (s[i][j]==k) return 0;

return 1;
}

void line(char *s)
/* 1 grid line */
{

int i;

loop (i,0,N)
printf("%c%c%c%c",s[i%S!=0],s,s,s);
putchar(s);

putchar('\n');
}

void prints(sudoku_t s, char *filename)
/* print sudoku s */
{
/* File pointers to the input file and the outputfile */
FILE *fp;
int x,y;
printf("Saving %s...", filename);

// sanity check - game file has to exist and be writable.
if ((fp = fopen(filename,"w")) == NULL) {
printf("Output file cannot be written!\n");
return 0;
}
else
loop (x,0,N) {
line(x%S?"| ":"+-");
loop (y,0,N)

fprintf(fp,"%c %c ",
y%S?' ':'|',
s[x][y]?s[x][y]<=9?s[x][y]+'0':s[x][y]+'@':' ');
fputs("|",fp); }

line("+-");
// write end of line
fprintf(fp, "\n");
}

/* Close the output file */
fclose (fp);
int nsol=0;

void recursive_search(sudoku_t s)
/*
scans sudoku for an empty field and tries to put a number 1..N there
if successfull, calls recursive_search again
*/
{
int x,y,k;

loop (x,0,N) loop (y,0,N) {
if (s[x][y]==0) {
loopto (k,1,N) if (valid(x,y,k,s)) {
/* s[x][y]=k is valid, so let's do it and try next */
sudoku_t news;

memcpy(news,s,sizeof(sudoku_t));
news[x][y]=k;
recursive_search(news); }
/* no digit fits s[x][y], so giving up... */
return; } }

/* none of s[x][y] is empty ==> we have a solution */
nsol++;
prints(s);
}

int main(int argc ,char **arg)
{
FILE *fp;
sudoku_t s;
int x,y;
char line;

if (argc<2) {
fprintf(stderr,"%dx%d Usage:\n\
sudoku {FILE|-}\n\
where FILE (- = stdin) has %d lines by %d chars\n\
1 char = {1234} for 4x4, {1..9} for 9x9, {1..9,A..P} for 16x16\n\
SPACE or 0 denotes a char to find\n\

}

else fp=fopen(arg,"rt");

if (!fp) Error(arg);

prints(s);

recursive_search(s);

printf("%d solution%s found\n",nsol,"s"+(nsol==1));

return 0;
}
```
any help will be greatly appreciated Last edited by a moderator: Nov 25, 2007
2. ### panayiotispatraNew Member

Joined:
Oct 25, 2007
Messages:
6
0
Trophy Points:
0
Code:
```void writePuzzle(sudoku_t s, char *filename) {

/* File pointers to the input file and the outputfile */
FILE *fp;
int i,j;

printf("Saving %s...", filename);

// sanity check - game file has to exist and be writable.
if ((fp = fopen(filename,"w")) == NULL) {
errorandexit("Output file cannot be written!\n");
}

for (i=0; i<N; i++) {
for (j=0; j<N; j++) {

/*
* write a number at-a-time to the file. We utilize abs()
* in order to remove the sign that indicates locked numbers
*/
fprintf(fp, "%d ", sudoku[i][j]);
}
// write end of line
fprintf(fp, "\n");
}

/* Close the output file */
fclose (fp);

printf("Done\n");

}```

Last edited by a moderator: Nov 28, 2007