您的位置:首页 > 编程语言 > C语言/C++

求解九位宫游戏的C语言核心代码

2009-09-08 07:54 176 查看
这是一个求解九位宫游戏的C语言核心代码:
/*查找空格*/
int find(int tab[9][9],int i,int j){
int l,k,count=1;
for(l=i;l<9;l++)
for(k=j;k<9;k++)
if(tab[l][k]==0){
backdata(tab,l,k,count);
}/*end if1 */
return tab;
}
/*回溯*/
int backdata(int tab[9][9],int i,int j,int count){
int l,k,mark = 0;
mark = solution(tab,i,j,count);
if(mark == 0){
low[topl] = i;
topl++;
low[topr] = j;
topr++;
find(tab,i,j);
}
if(mark == 1){
l = low[topl];
topl--;
k = row[topr];
topr--;
count = tab[l][k];
backdata(tab,l,k,count);
}
}
/*为空格寻找合适的数字*/
int solution(int tab[9][9],int low,int row,int count){
int m1,m2,i,mark;
for(i=count;i<=9;i++){
m1 = decideRL(tab,i,low,row);
m2 = decideCHECK(tab,i,low,row);
if((m1 == 0)&&(m2 == 0)){
tab[low][row] = i;
mark = 0;
}
else mark = 1;
}
return mark;
}
/*判断横和列*/
int decideRL(int tab[9][9],int value,int low,int row){
int j,mark=0;
for(j=0;j<9;j++)
if((tab[low][j]== value)||(tab[j][row])==value)
mark = 1;
return mark;
}
/* 判断小方正 */
int decideCHECK(int tab[9][9],int value,int low,int row){
int i,j,L,R,ls,ln,rs,rn,mark=0;
L = low/3;
R = row/3;
if(L-1<=0)
ls = 0;
ls = L*3;
ln = (L+1)*3-1;
if(R-1<=0)
rs = 0;
rs = R*3;
rn = (R+1)*3-1;
for(i=ls;i<=ln;i++)
for(j=rs;j<=rn;j++)
if(tab[j] == value)
mark = 1;
return mark;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: