sunasunaxの日記

制約論理プログラム iZ-C の紹介

魔方陣を制約論理プログラム iZ-Cを使って技巧的に解きます。

方陣を制約論理プログラム iZ-Cを使って技巧的に解きます。
-----------------------------------------------------------------------------
make && ./magicsqure_simp 4 |less
Sun Aug 23 07:03:24 2020
{0..13}, {0..15}, {0..15}, {1..14}, {0..15}, {0..15}, {0..15}, {0..15}, {0..15}, {0..15}, {0..15}, {0..15}, {2..15}, {0..15}, {0..15}, {1..15}
ESC[2JESC[0;0fESC[0;0f
Solution 1
0 14 13 3
11 5 6 8
7 9 10 4
12 2 1 15
ESC[0;0f
Solution 2
0 13 14 3
11 6 5 8
7 10 9 4
12 1 2 15
..........................
Solution 880
6 3 13 8
10 15 1 4
5 12 2 11
9 0 14 7

Nb Fails = 39353
Nb Choice Points = 47076
Heap Size = 1024
Elapsed Time = 0.078023 s
Sun Aug 23 07:03:45 2020
---------------------------------------------------------------------------------
/**************************************************************************
* 魔方陣
**************************************************************************/
#include <stdlib.h>
#include <time.h>
#include "iz.h"

int DIM, LIM_MAIN;
/**************************************************************************/
void printSolution(CSint **allvars, int nbVars) {
int i, val;
static int NbSolutions = 0;

printf("\033[0;0f");
printf("\nSolution %d\n", ++NbSolutions);
for (i = 0; i < DIM*DIM; i++) {
val = cs_getValue(allvars[i]);
printf("%3d", val);
if*1;
DIM = atoi(argv[1]); LIM_MAIN = DIM*(DIM*DIM-1)/2;

CSint **CELL = (CSint **)malloc(DIM*DIM * sizeof(CSint *));

CSint ***RC_CELL = (CSint ***)malloc(DIM * sizeof(CSint **));
CSint ***CR_CELL = (CSint ***)malloc(DIM * sizeof(CSint **));
for(i = 0; i < DIM; i++){
RC_CELL[i] = (CSint **)malloc(DIM * sizeof(CSint *));
CR_CELL[i] = (CSint **)malloc(DIM * sizeof(CSint *));
}

CSint ***dRL = (CSint ***)malloc(2 * sizeof(CSint **));
for(i = 0; i < 2; i++){
dRL[i] = (CSint **)malloc(DIM * sizeof(CSint *));
}

int n=0;
for(i = 0; i < DIM; i++){
for(j = 0; j < DIM; j++){
CELL[n++] = RC_CELL[i][j] = CR_CELL[j][i] = cs_createCSint(0, DIM*DIM-1);
}
}

for(i = 0; i < DIM; i++){
cs_EQ(cs_Sigma(RC_CELL[i], DIM), LIM_MAIN);
cs_EQ(cs_Sigma(CR_CELL[i], DIM), LIM_MAIN);
}

for(i = 0; i < DIM; i++){
dRL[0][i] = RC_CELL[i][i];
dRL[1][i] = RC_CELL[i][DIM - 1 - i];
}
cs_AllNeq(&CELL[0], DIM*DIM);

for(i = 0; i < 2; i++){
cs_EQ(cs_Sigma(dRL[i], DIM), LIM_MAIN);
}

cs_Lt(RC_CELL[0][0], RC_CELL[0][DIM-1]);
cs_Lt(RC_CELL[0][DIM-1], RC_CELL[DIM-1][0]);
cs_Lt(RC_CELL[0][0], RC_CELL[DIM-1][DIM-1]);

cs_printf("%A\n", CELL, DIM*DIM);

printf("\033[2J\033[0;0f"); /* clear screen */
cs_findAll(&CELL[0], DIM*DIM, cs_findFreeVarNbConstraints, printSolution);
// cs_findAll(&CELL[0], DIM*DIM, cs_findFreeVar, printSolution);

cs_printStats();
printf("Elapsed Time = %g s\n", (double) (clock() - t0) / CLOCKS_PER_SEC);
time(&nowtime); printf("%s", ctime(&nowtime));
cs_end();
return EXIT_SUCCESS;
}

 

*1:i+1) % DIM == 0) putchar('\n');
}
}
/**************************************************************************/
/**************************************************************************/
int main(int argc, char **argv) {
int i, j;
clock_t t0 = clock();
time_t nowtime;

cs_init();
time(&nowtime); printf("%s", ctime(&nowtime