永发信息网

求16宫格C程序算法

答案:2  悬赏:0  手机版
解决时间 2021-02-24 19:24
如题,想了很久,用了最笨的办法,但需要时间太长(n天),希望有高手提供精辟的算法,在短时间内求解所有16宫格,要求用C语言或C++语言,所给代码可以运行出结果。ps:16宫格与9宫格类似,在4*4矩阵中,4行与4列与2对角线这10个各自的累加值均相等,且这16个数是从1~16选出,不可重复,即每个数用且仅用一次。我的积分本来就不多,只能提供一点咯,望高手指点~不胜感激~
最佳答案
N阶幻方构造#include
using namespace std;//幻方构造(l!=2)
#define MAXN 100void dllb(int l,int si,int sj,int sn,int d[][MAXN]){
int n,i=0,j=l/2;
for (n=1;n<=l*l;n++){
d[i+si][j+sj]=n+sn;
if (n%l){
i=(i)?(i-1):(l-1);
j=(j==l-1)?0:(j+1);
}
else
i=(i==l-1)?0:(i+1);
}
}void magic_odd(int l,int d[][MAXN]){
dllb(l,0,0,0,d);
}void magic_4k(int l,int d[][MAXN]){
int i,j;
for (i=0;i for (j=0;j d[i][j]=((i%4==0||i%4==3)&&(j%4==0||j%4==3)||(i%4==1||i%4==2)&&(j%4==1||j%4==2))?(l*l-(i*l+j)):(i*l+j+1);
}void magic_other(int l,int d[][MAXN]){
int i,j,t;
dllb(l/2,0,0,0,d);
dllb(l/2,l/2,l/2,l*l/4,d);
dllb(l/2,0,l/2,l*l/2,d);
dllb(l/2,l/2,0,l*l/4*3,d);
for (i=0;i for (j=0;j if (i!=l/4||j)
t=d[i][j],d[i][j]=d[i+l/2][j],d[i+l/2][j]=t;
t=d[l/4][l/4],d[l/4][l/4]=d[l/4+l/2][l/4],d[l/4+l/2][l/4]=t;
for (i=0;i for (j=l-l/4+1;j t=d[i][j],d[i][j]=d[i+l/2][j],d[i+l/2...N阶幻方构造#include
using namespace std;//幻方构造(l!=2)
#define MAXN 100void dllb(int l,int si,int sj,int sn,int d[][MAXN]){
int n,i=0,j=l/2;
for (n=1;n<=l*l;n++){
d[i+si][j+sj]=n+sn;
if (n%l){
i=(i)?(i-1):(l-1);
j=(j==l-1)?0:(j+1);
}
else
i=(i==l-1)?0:(i+1);
}
}void magic_odd(int l,int d[][MAXN]){
dllb(l,0,0,0,d);
}void magic_4k(int l,int d[][MAXN]){
int i,j;
for (i=0;i for (j=0;j d[i][j]=((i%4==0||i%4==3)&&(j%4==0||j%4==3)||(i%4==1||i%4==2)&&(j%4==1||j%4==2))?(l*l-(i*l+j)):(i*l+j+1);
}void magic_other(int l,int d[][MAXN]){
int i,j,t;
dllb(l/2,0,0,0,d);
dllb(l/2,l/2,l/2,l*l/4,d);
dllb(l/2,0,l/2,l*l/2,d);
dllb(l/2,l/2,0,l*l/4*3,d);
for (i=0;i for (j=0;j if (i!=l/4||j)
t=d[i][j],d[i][j]=d[i+l/2][j],d[i+l/2][j]=t;
t=d[l/4][l/4],d[l/4][l/4]=d[l/4+l/2][l/4],d[l/4+l/2][l/4]=t;
for (i=0;i for (j=l-l/4+1;j t=d[i][j],d[i][j]=d[i+l/2][j],d[i+l/2][j]=t;
}void generate(int l,int d[][MAXN]){
if (l%2)
magic_odd(l,d);
else if (l%4==0)
magic_4k(l,d);
else
magic_other(l,d);
}int main() {
int i, j, mat[MAXN][MAXN];
generate(4, mat);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%d ", mat[i][j]);
}
putchar('\n');
}
system("pause");
}
全部回答
#include #include static int pu[9][9]= { {0,0,0,7,2,8,0,0,0}, {0,9,0,0,5,1,6,0,0}, {0,0,0,0,6,0,0,8,2}, {3,0,0,8,0,2,7,0,4}, {1,7,4,0,3,0,0,2,0}, {2,8,0,5,0,0,0,3,0}, {0,1,0,3,0,0,2,0,0}, {0,0,7,0,4,6,0,0,5}, {0,0,6,1,0,0,0,4,9} }; int isvalid(const int i, const int j)//验证函数当期i,j坐标是否符合游戏规则,不重复 { const int n = pu[i][j]; const static int query[] = {0, 0, 0, 3, 3, 3, 6, 6, 6}; int t, u; for (t = 0; t < 9; t++) if (t != i && pu[t][j] == n || t != j && pu[i][t] == n)//0-9的数字,每行每列都不能重复 return 0; for (t = query[i]; t < query[i] + 3; t++) //9个宫的3×3里也不能重复 for (u = query[j]; u < query[j] + 3; u++) if ((t != i || u != j) && pu[t][u] == n) return 0; return 1; } void output(void)//输入函数 { static int n; cout << "Solution " << ...#include #include static int pu[9][9]= { {0,0,0,7,2,8,0,0,0}, {0,9,0,0,5,1,6,0,0}, {0,0,0,0,6,0,0,8,2}, {3,0,0,8,0,2,7,0,4}, {1,7,4,0,3,0,0,2,0}, {2,8,0,5,0,0,0,3,0}, {0,1,0,3,0,0,2,0,0}, {0,0,7,0,4,6,0,0,5}, {0,0,6,1,0,0,0,4,9} }; int isvalid(const int i, const int j)//验证函数当期i,j坐标是否符合游戏规则,不重复 { const int n = pu[i][j]; const static int query[] = {0, 0, 0, 3, 3, 3, 6, 6, 6}; int t, u; for (t = 0; t < 9; t++) if (t != i && pu[t][j] == n || t != j && pu[i][t] == n)//0-9的数字,每行每列都不能重复 return 0; for (t = query[i]; t < query[i] + 3; t++) //9个宫的3×3里也不能重复 for (u = query[j]; u < query[j] + 3; u++) if ((t != i || u != j) && pu[t][u] == n) return 0; return 1; } void output(void)//输入函数 { static int n; cout << "Solution " << ++n << ":" <
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯