主题:用FPE制作的数独游戏
我是学生
[专家分:0] 发布于 2006-11-14 22:40:00
out
最后更新于:2007-01-23 09:58:00
回复列表 (共26个回复)
11 楼
maxumi [专家分:2200] 发布于 2006-11-15 13:55:00
是原创的么?
12 楼
angwuy [专家分:2280] 发布于 2006-11-15 14:43:00
楼主辛苦了
13 楼
maxumi [专家分:2200] 发布于 2006-11-15 15:09:00
加精以示鼓励XD
14 楼
我是学生 [专家分:0] 发布于 2006-11-15 18:43:00
对。
其实我是有一点问题想请教大家。
15 楼
编程黑客 [专家分:1660] 发布于 2006-11-17 09:54:00
什么问题啊
不过我觉得能有耐心编出这么长的程序,好厉害啊
16 楼
yexin218 [专家分:4070] 发布于 2006-11-19 08:57:00
不明白:
我觉得这个不错:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SUDOKU_SIZE 9
#define SBIT(X) (1<<(X))
typedef struct sudoku
{
int overlay[SUDOKU_SIZE][SUDOKU_SIZE];
int rowmask[SUDOKU_SIZE];
int colmask[SUDOKU_SIZE];
} sudoku;
int sudoku_solve(sudoku *s);
int main(int arg, char *argv[])
{
int n,z;
sudoku puzzle={
{{0,6,0,2,0,4,0,5,0},
{4,7,9,0,6,0,0,8,3},
{0,2,5,0,7,0,1,0,0},
{9,0,0,1,0,3,0,0,2},
{0,1,2,0,0,6,3,4,0},
{6,3,0,7,0,9,0,0,8},
{0,0,6,0,8,0,7,0,1},
{1,4,0,0,9,0,0,2,5},
{0,8,0,3,0,5,0,9,0}},
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0} };
if(sudoku_solve(&puzzle)>=0)
{
int row,col;
printf("\aSolved!\n\n");
for(row=0; row<SUDOKU_SIZE; row++,printf("\n"))
for(col=0; col<SUDOKU_SIZE; col++)
printf("\t%d",puzzle.overlay[row][col]);
}
scanf("%d",&z);
return(0);
}
void sudoku_generatemask(sudoku *s);
int sudoku_rc_inc(int *row, int *col)
{
(*col)++;
if((*col)>=SUDOKU_SIZE)
{
(*row)++;
(*col)=0;
}
return((*row)<SUDOKU_SIZE);
}
void sudoku_set(struct sudoku *s,int r, int c, int val)
{
// fprintf(stderr,"Setting (%d,%d)=%d\n",r,c,val);
if(s->rowmask[r] & SBIT(val))
{
fprintf(stderr,"Row Collision in (%d,%d)\n",r,c);
abort();
}
if(s->colmask[c] & SBIT(val))
{
fprintf(stderr,"Col Collision in (%d,%d)\n",r,c);
abort();
}
s->rowmask[r]|=SBIT(val);
s->colmask[c]|=SBIT(val);
s->overlay[r][c]=val;
}
int sudoku_solve_i(sudoku s, int r, int c, sudoku *out)
{
int v;
int pvalues;
if(r>=SUDOKU_SIZE)
{
(*out)=s;
return(0);
}
while(s.overlay[r][c])
if(sudoku_rc_inc(&r,&c)==0) break;
if(r>=SUDOKU_SIZE)
{
(*out)=s;
return(0);
}
for(v=1; v<=SUDOKU_SIZE; v++)
{
if(SBIT(v) & (s.rowmask[r] | s.colmask[c])) continue;
{
int r2=r;
int c2=c;
sudoku s2=s;
sudoku_set(&s2,r,c,v);
sudoku_rc_inc(&r2,&c2);
if(sudoku_solve_i(s2,r2,c2,out)>=0)
return(0);
}
}
return(-1);
}
int sudoku_solve(struct sudoku *s)
{
sudoku_generatemask(s);
return(sudoku_solve_i(*s,0,0,s));
// return(-1);
}
void sudoku_generatemask(sudoku *s)
{
int r,c;
memset(s->rowmask,0,sizeof(s->rowmask));
memset(s->colmask,0,sizeof(s->colmask));
for(r=0; r<SUDOKU_SIZE; r++)
for(c=0; c<SUDOKU_SIZE; c++)
if(s->overlay[r][c])
sudoku_set(s,r,c,s->overlay[r][c]);
}
http://blog.chinaunix.net/u/21684/showart_200370.html
17 楼
yexin218 [专家分:4070] 发布于 2006-11-19 08:58:00
http://blog.chinaunix.net/u/21684/showart_200370.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SUDOKU_SIZE 9
#define SBIT(X) (1<<(X))
typedef struct sudoku
{
int overlay[SUDOKU_SIZE][SUDOKU_SIZE];
int rowmask[SUDOKU_SIZE];
int colmask[SUDOKU_SIZE];
} sudoku;
int sudoku_solve(sudoku *s);
int main(int arg, char *argv[])
{
int n,z;
sudoku puzzle={
{{0,6,0,2,0,4,0,5,0},
{4,7,9,0,6,0,0,8,3},
{0,2,5,0,7,0,1,0,0},
{9,0,0,1,0,3,0,0,2},
{0,1,2,0,0,6,3,4,0},
{6,3,0,7,0,9,0,0,8},
{0,0,6,0,8,0,7,0,1},
{1,4,0,0,9,0,0,2,5},
{0,8,0,3,0,5,0,9,0}},
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0} };
if(sudoku_solve(&puzzle)>=0)
{
int row,col;
printf("\aSolved!\n\n");
for(row=0; row<SUDOKU_SIZE; row++,printf("\n"))
for(col=0; col<SUDOKU_SIZE; col++)
printf("\t%d",puzzle.overlay[row][col]);
}
scanf("%d",&z);
return(0);
}
void sudoku_generatemask(sudoku *s);
int sudoku_rc_inc(int *row, int *col)
{
(*col)++;
if((*col)>=SUDOKU_SIZE)
{
(*row)++;
(*col)=0;
}
return((*row)<SUDOKU_SIZE);
}
void sudoku_set(struct sudoku *s,int r, int c, int val)
{
// fprintf(stderr,"Setting (%d,%d)=%d\n",r,c,val);
if(s->rowmask[r] & SBIT(val))
{
fprintf(stderr,"Row Collision in (%d,%d)\n",r,c);
abort();
}
if(s->colmask[c] & SBIT(val))
{
fprintf(stderr,"Col Collision in (%d,%d)\n",r,c);
abort();
}
s->rowmask[r]|=SBIT(val);
s->colmask[c]|=SBIT(val);
s->overlay[r][c]=val;
}
int sudoku_solve_i(sudoku s, int r, int c, sudoku *out)
{
int v;
int pvalues;
if(r>=SUDOKU_SIZE)
{
(*out)=s;
return(0);
}
while(s.overlay[r][c])
if(sudoku_rc_inc(&r,&c)==0) break;
if(r>=SUDOKU_SIZE)
{
(*out)=s;
return(0);
}
for(v=1; v<=SUDOKU_SIZE; v++)
{
if(SBIT(v) & (s.rowmask[r] | s.colmask[c])) continue;
{
int r2=r;
int c2=c;
sudoku s2=s;
sudoku_set(&s2,r,c,v);
sudoku_rc_inc(&r2,&c2);
if(sudoku_solve_i(s2,r2,c2,out)>=0)
return(0);
}
}
return(-1);
}
int sudoku_solve(struct sudoku *s)
{
sudoku_generatemask(s);
return(sudoku_solve_i(*s,0,0,s));
// return(-1);
}
void sudoku_generatemask(sudoku *s)
{
int r,c;
memset(s->rowmask,0,sizeof(s->rowmask));
memset(s->colmask,0,sizeof(s->colmask));
for(r=0; r<SUDOKU_SIZE; r++)
for(c=0; c<SUDOKU_SIZE; c++)
if(s->overlay[r][c])
sudoku_set(s,r,c,s->overlay[r][c]);
}
这个不错。
18 楼
zjsyzhong [专家分:520] 发布于 2006-11-24 17:54:00
这么长!我都没耐心看拉
[em19]
19 楼
xmj531 [专家分:30] 发布于 2006-11-25 13:48:00
厉害我看的我都有点点头
真有赖心我要象你学习
20 楼
freeeerf [专家分:5440] 发布于 2006-11-29 14:17:00
好!
我来回复