gpt4 book ai didi

c - 加速 C 中的 hexadoku(16 x 16,a - p)

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:01 25 4
gpt4 key购买 nike

我的六边形求解器有严重问题。我想在 2 秒内运行该程序,但我的解决方案在 cca 10 秒内解决了它。另一个问题是如果 hexadoku 有不止一个解决方案,我应该如何只打印解决方案的数量。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include <stdio.h>
#include <stdio.h>

int isfree(int hexadoku[][16], int radek, int sloupec, int num)
{
int rowStart = (radek/4) * 4;
int colStart = (sloupec/4) * 4;

int i;
for(i=0; i<16; ++i)
{
if (hexadoku[radek][i] == num) return 0;
if (hexadoku[i][sloupec] == num) return 0;
if (hexadoku[rowStart + (i%4)][colStart + (i/4)] == num) return 0;
}

return 1;
}

int napln(int hexadoku[][16], int radek, int sloupec)
{
int i = 0;
if (sloupec >= 16)
{
sloupec = 0;
++radek;
if (radek >= 16)
{
return 1;
}
}

if( hexadoku[radek][sloupec] != 0)
{
return napln(hexadoku, radek, sloupec+1);
}
else
{
for(i=0; i<16; ++i)
{
if( isfree(hexadoku, radek, sloupec, i+1) )
{
hexadoku[radek][sloupec] = i+1;

int good = napln(hexadoku, radek, sloupec +1);
if (good)
{ return 1;
}
hexadoku[radek][sloupec] = 0;
}
}
}
return 0;
}

int main()
{

char pole[17][17];
char tmp[66];
int i = 0, j = 0, k = 0, l = 0, m = 0, x = 0, y = 0;

printf("Zadejte hexadoku:\n");
while ( scanf( " %[^\n]", &tmp ) == 1 )
{

for ( j = 2; j <= 62; j += 4 )
{
if ( tmp[0] != '+' )
{
pole[i][k] = tmp[j];
k++;
}
}

pole[i][k] = '\0';
k = 0;
if ( tmp[0] != '+' )
i++;
}

for ( i = 0; i < 16; i++ )
{
for ( l = 0; l < 16; l++ )
{
if( !(( pole[i][l] >='a' && pole[i][l] <= 'p' ) || ( pole[i][l] == ' ' )) ) { printf("Nespravny vstup.\n"); return 1; }
}
}

i = 0;
l = 0;

for (m = 0; m < 16; m++)
{
for (i = 0; i < 16; i++)
{
for (l = 0; l < 16; l++)
{
if ((pole[m][l] == pole[m][i]) && (i != l) && (pole[m][l] != ' ') && (pole[m][i] != ' ') ) { printf("Nespravny vstup.\n"); return 1; }
if ((pole[l][m] == pole[i][m]) && (i != l) && (pole[l][m] != ' ') && (pole[i][m] != ' ') ) { printf("Nespravny vstup.\n"); return 1; }
}
}
}

i = 0;
l = 0;
j = 0;

for (l = 0; l < 16; l+=3)
{
for (i = 0; i < 4 ; i++)
{
for (j = 0; j < 4; j++)
{
for (x = 0; x < 4; x++)
{
for (y = 0; y < 4; y++)
{
if(i==x && j==y) continue;
if ( (pole[i][j] == pole[x][y]) && (pole[i][j] != ' ') && (pole[x][y] != ' ') ) { printf("Nespravny vstup.\n"); return 1; }
}
}
}
}
i+=3;
j+=3;
x+=3;
y+=3;
}

int hexadoku[16][16];


for (i = 0; i < 16; i++)
{
for (j = 0; j < 16; j++)
{
switch (pole[i][j])
{
case ' ': hexadoku[i][j]=0; break;
case 'a': hexadoku[i][j]=1; break;
case 'b': hexadoku[i][j]=2; break;
case 'c': hexadoku[i][j]=3; break;
case 'd': hexadoku[i][j]=4; break;
case 'e': hexadoku[i][j]=5; break;
case 'f': hexadoku[i][j]=6; break;
case 'g': hexadoku[i][j]=7; break;
case 'h': hexadoku[i][j]=8; break;
case 'i': hexadoku[i][j]=9; break;
case 'j': hexadoku[i][j]=10; break;
case 'k': hexadoku[i][j]=11; break;
case 'l': hexadoku[i][j]=12; break;
case 'm': hexadoku[i][j]=13; break;
case 'n': hexadoku[i][j]=14; break;
case 'o': hexadoku[i][j]=15; break;
case 'p': hexadoku[i][j]=16; break;
default : break;
}
}
}

if( napln(hexadoku, 0, 0) )
{

for (i = 0; i < 16; i++)
{
for (j = 0; j < 16; j++)
{
switch (hexadoku[i][j])
{
case 0: pole[i][j]= ' '; break;
case 1: pole[i][j]= 'a'; break;
case 2: pole[i][j]= 'b'; break;
case 3: pole[i][j]= 'c'; break;
case 4: pole[i][j]= 'd'; break;
case 5: pole[i][j]= 'e'; break;
case 6: pole[i][j]= 'f'; break;
case 7: pole[i][j]= 'g'; break;
case 8: pole[i][j]= 'h'; break;
case 9: pole[i][j]= 'i'; break;
case 10: pole[i][j]= 'j'; break;
case 11: pole[i][j]= 'k'; break;
case 12: pole[i][j]= 'l'; break;
case 13: pole[i][j]= 'm'; break;
case 14: pole[i][j]= 'n'; break;
case 15: pole[i][j]= 'o'; break;
case 16: pole[i][j]= 'p'; break;
default : break;
}
}
}

printf(
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"

, pole[0][0], pole[0][1], pole[0][2], pole[0][3], pole[0][4], pole[0][5], pole[0][6], pole[0][7], pole[0][8], pole[0][9], pole[0][10], pole[0][11], pole[0][12], pole[0][13], pole[0][14], pole[0][15]
, pole[1][0], pole[1][1], pole[1][2], pole[1][3], pole[1][4], pole[1][5], pole[1][6], pole[1][7], pole[1][8], pole[1][9], pole[1][10], pole[1][11], pole[1][12], pole[1][13], pole[1][14], pole[1][15]
, pole[2][0], pole[2][1], pole[2][2], pole[2][3], pole[2][4], pole[2][5], pole[2][6], pole[2][7], pole[2][8], pole[2][9], pole[2][10], pole[2][11], pole[2][12], pole[2][13], pole[2][14], pole[2][15]
, pole[3][0], pole[3][1], pole[3][2], pole[3][3], pole[3][4], pole[3][5], pole[3][6], pole[3][7], pole[3][8], pole[3][9], pole[3][10], pole[3][11], pole[3][12], pole[3][13], pole[3][14], pole[3][15]
, pole[4][0], pole[4][1], pole[4][2], pole[4][3], pole[4][4], pole[4][5], pole[4][6], pole[4][7], pole[4][8], pole[4][9], pole[4][10], pole[4][11], pole[4][12], pole[4][13], pole[4][14], pole[4][15]
, pole[5][0], pole[5][1], pole[5][2], pole[5][3], pole[5][4], pole[5][5], pole[5][6], pole[5][7], pole[5][8], pole[5][9], pole[5][10], pole[5][11], pole[5][12], pole[5][13], pole[5][14], pole[5][15]
, pole[6][0], pole[6][1], pole[6][2], pole[6][3], pole[6][4], pole[6][5], pole[6][6], pole[6][7], pole[6][8], pole[6][9], pole[6][10], pole[6][11], pole[6][12], pole[6][13], pole[6][14], pole[6][15]
, pole[7][0], pole[7][1], pole[7][2], pole[7][3], pole[7][4], pole[7][5], pole[7][6], pole[7][7], pole[7][8], pole[7][9], pole[7][10], pole[7][11], pole[7][12], pole[7][13], pole[7][14], pole[7][15]
, pole[8][0], pole[8][1], pole[8][2], pole[8][3], pole[8][4], pole[8][5], pole[8][6], pole[8][7], pole[8][8], pole[8][9], pole[8][10], pole[8][11], pole[8][12], pole[8][13], pole[8][14], pole[8][15]
, pole[9][0], pole[9][1], pole[9][2], pole[9][3], pole[9][4], pole[9][5], pole[9][6], pole[9][7], pole[9][8], pole[9][9], pole[9][10], pole[9][11], pole[9][12], pole[9][13], pole[9][14], pole[9][15]
, pole[10][0], pole[10][1], pole[10][2], pole[10][3], pole[10][4], pole[10][5], pole[10][6], pole[10][7], pole[10][8], pole[10][9], pole[10][10], pole[10][11], pole[10][12], pole[10][13], pole[10][14], pole[10][15]
, pole[11][0], pole[11][1], pole[11][2], pole[11][3], pole[11][4], pole[11][5], pole[11][6], pole[11][7], pole[11][8], pole[11][9], pole[11][10], pole[11][11], pole[11][12], pole[11][13], pole[11][14], pole[11][15]
, pole[12][0], pole[12][1], pole[12][2], pole[12][3], pole[12][4], pole[12][5], pole[12][6], pole[12][7], pole[12][8], pole[12][9], pole[12][10], pole[12][11], pole[12][12], pole[12][13], pole[12][14], pole[12][15]
, pole[13][0], pole[13][1], pole[13][2], pole[13][3], pole[13][4], pole[13][5], pole[13][6], pole[13][7], pole[13][8], pole[13][9], pole[13][10], pole[13][11], pole[13][12], pole[13][13], pole[13][14], pole[13][15]
, pole[14][0], pole[14][1], pole[14][2], pole[14][3], pole[14][4], pole[14][5], pole[14][6], pole[14][7], pole[14][8], pole[14][9], pole[14][10], pole[14][11], pole[14][12], pole[14][13], pole[14][14], pole[14][15]
, pole[15][0], pole[15][1], pole[15][2], pole[15][3], pole[15][4], pole[15][5], pole[15][6], pole[15][7], pole[15][8], pole[15][9], pole[15][10], pole[15][11], pole[15][12], pole[15][13], pole[15][14], pole[15][15]);


}
else
{
printf("Reseni neexistuje.\n");
}

return 0;
}

这是我的原始代码。

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| h | p m o | i k | n f l |
+ + + + + + + + + + + + + + + + +
| c l g m | n e | h p | o d |
+ + + + + + + + + + + + + + + + +
| o | d | b | |
+ + + + + + + + + + + + + + + + +
| p i e | h g | m | b |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| c | h | n k f | i g |
+ + + + + + + + + + + + + + + + +
| h | l p | i b j | c |
+ + + + + + + + + + + + + + + + +
| f a | j g | p | |
+ + + + + + + + + + + + + + + + +
| e | i o | l | h p n |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| i d | n | o a g b | l |
+ + + + + + + + + + + + + + + + +
| f j g | m | d i | c |
+ + + + + + + + + + + + + + + + +
| l k | | f | o |
+ + + + + + + + + + + + + + + + +
| | | m | k n |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| n | b k | g o d | c h i |
+ + + + + + + + + + + + + + + + +
| o | l | n | m |
+ + + + + + + + + + + + + + + + +
| h g f | j | p | e d |
+ + + + + + + + + + + + + + + + +
| | m p f | c j | b |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

这里是输入数据。

感谢您的任何建议!

最佳答案

不必总是编写自己的求解器,您可以使用像 Gecode 这样专为这些任务编写的约束规划求解器。

#include <gecode/int.hh>
#include <gecode/minimodel.hh>
#include <gecode/search.hh>

using namespace Gecode;

class Hexadoka : public Space {
protected:
IntVarArray l;
public:
Hexadoka(int hexadoku[16][16]) : l(* this, 16*16, 0, 15) {
Matrix<IntVarArgs> m(l,16,16);
for (int i=0; i<16; i++) {
distinct(*this, m.row(i)); //all rows must contain distinct values
distinct(*this, m.col(i)); //all columns must contain distinct values
for (int j=0; j<16; j++) {
if(hexadoku[i][j] != -1) {
rel(*this, m(j,i), IRT_EQ, hexadoku[i][j]); //set already known values
}
}
}
for (int i=0; i<16; i += 4) {
for (int j=0; j<16; j += 4) {
distinct(*this, m.slice(i, i+4, j, j+4)); //all blocks must contain distinct values
}
}
branch(* this, l, INT_VAR_SIZE_MIN(), INT_VAL_MIN()); //set a brancher
}
Hexadoka(bool share, Hexadoka& s) : Space(share, s) {
l.update(* this, share, s.l);
}
virtual Space* copy(bool share) {
return new Hexadoka(share,* this);
}
char vl (int i, int j) const {
int ofs = 16*i+j;
if(l[ofs].assigned()) {
return 'a'+l[ofs].val();
} else {
return ' ';
}
}

void print(void) const {
printf(
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+ + + + + + + + + + + + + + + + +\n"
"| %c %c %c %c | %c %c %c %c | %c %c %c %c | %c %c %c %c |\n"
"+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"

, vl(0,0), vl(0,1), vl(0,2), vl(0,3), vl(0,4), vl(0,5), vl(0,6), vl(0,7), vl(0,8), vl(0,9), vl(0,10), vl(0,11), vl(0,12), vl(0,13), vl(0,14), vl(0,15)
, vl(1,0), vl(1,1), vl(1,2), vl(1,3), vl(1,4), vl(1,5), vl(1,6), vl(1,7), vl(1,8), vl(1,9), vl(1,10), vl(1,11), vl(1,12), vl(1,13), vl(1,14), vl(1,15)
, vl(2,0), vl(2,1), vl(2,2), vl(2,3), vl(2,4), vl(2,5), vl(2,6), vl(2,7), vl(2,8), vl(2,9), vl(2,10), vl(2,11), vl(2,12), vl(2,13), vl(2,14), vl(2,15)
, vl(3,0), vl(3,1), vl(3,2), vl(3,3), vl(3,4), vl(3,5), vl(3,6), vl(3,7), vl(3,8), vl(3,9), vl(3,10), vl(3,11), vl(3,12), vl(3,13), vl(3,14), vl(3,15)
, vl(4,0), vl(4,1), vl(4,2), vl(4,3), vl(4,4), vl(4,5), vl(4,6), vl(4,7), vl(4,8), vl(4,9), vl(4,10), vl(4,11), vl(4,12), vl(4,13), vl(4,14), vl(4,15)
, vl(5,0), vl(5,1), vl(5,2), vl(5,3), vl(5,4), vl(5,5), vl(5,6), vl(5,7), vl(5,8), vl(5,9), vl(5,10), vl(5,11), vl(5,12), vl(5,13), vl(5,14), vl(5,15)
, vl(6,0), vl(6,1), vl(6,2), vl(6,3), vl(6,4), vl(6,5), vl(6,6), vl(6,7), vl(6,8), vl(6,9), vl(6,10), vl(6,11), vl(6,12), vl(6,13), vl(6,14), vl(6,15)
, vl(7,0), vl(7,1), vl(7,2), vl(7,3), vl(7,4), vl(7,5), vl(7,6), vl(7,7), vl(7,8), vl(7,9), vl(7,10), vl(7,11), vl(7,12), vl(7,13), vl(7,14), vl(7,15)
, vl(8,0), vl(8,1), vl(8,2), vl(8,3), vl(8,4), vl(8,5), vl(8,6), vl(8,7), vl(8,8), vl(8,9), vl(8,10), vl(8,11), vl(8,12), vl(8,13), vl(8,14), vl(8,15)
, vl(9,0), vl(9,1), vl(9,2), vl(9,3), vl(9,4), vl(9,5), vl(9,6), vl(9,7), vl(9,8), vl(9,9), vl(9,10), vl(9,11), vl(9,12), vl(9,13), vl(9,14), vl(9,15)
, vl(10,0), vl(10,1), vl(10,2), vl(10,3), vl(10,4), vl(10,5), vl(10,6), vl(10,7), vl(10,8), vl(10,9), vl(10,10), vl(10,11), vl(10,12), vl(10,13), vl(10,14), vl(10,15)
, vl(11,0), vl(11,1), vl(11,2), vl(11,3), vl(11,4), vl(11,5), vl(11,6), vl(11,7), vl(11,8), vl(11,9), vl(11,10), vl(11,11), vl(11,12), vl(11,13), vl(11,14), vl(11,15)
, vl(12,0), vl(12,1), vl(12,2), vl(12,3), vl(12,4), vl(12,5), vl(12,6), vl(12,7), vl(12,8), vl(12,9), vl(12,10), vl(12,11), vl(12,12), vl(12,13), vl(12,14), vl(12,15)
, vl(13,0), vl(13,1), vl(13,2), vl(13,3), vl(13,4), vl(13,5), vl(13,6), vl(13,7), vl(13,8), vl(13,9), vl(13,10), vl(13,11), vl(13,12), vl(13,13), vl(13,14), vl(13,15)
, vl(14,0), vl(14,1), vl(14,2), vl(14,3), vl(14,4), vl(14,5), vl(14,6), vl(14,7), vl(14,8), vl(14,9), vl(14,10), vl(14,11), vl(14,12), vl(14,13), vl(14,14), vl(14,15)
, vl(15,0), vl(15,1), vl(15,2), vl(15,3), vl(15,4), vl(15,5), vl(15,6), vl(15,7), vl(15,8), vl(15,9), vl(15,10), vl(15,11), vl(15,12), vl(15,13), vl(15,14), vl(15,15));
}
};

int main () {
char pole[17][17];
char tmp[66];
int i = 0, j = 0, k = 0, l = 0, m = 0, x = 0, y = 0;

printf("Zadejte hexadoku:\n");
while ( scanf( " %[^\n]", &tmp ) == 1 ) {
for ( j = 2; j <= 62; j += 4 ) {
if ( tmp[0] != '+' ) {
pole[i][k] = tmp[j];
k++;
}
}
pole[i][k] = '\0';
k = 0;
if ( tmp[0] != '+' )
i++;
}
for ( i = 0; i < 16; i++ ) {
for ( l = 0; l < 16; l++ ) {
if( !(( pole[i][l] >='a' && pole[i][l] <= 'p' ) || ( pole[i][l] == ' ' )) ) { printf("Nespravny vstup.\n"); return 1; }
}
}
i = 0;
l = 0;
for (m = 0; m < 16; m++) {
for (i = 0; i < 16; i++) {
for (l = 0; l < 16; l++) {
if ((pole[m][l] == pole[m][i]) && (i != l) && (pole[m][l] != ' ') && (pole[m][i] != ' ') ) { printf("Nespravny vstup.\n"); return 1; }
if ((pole[l][m] == pole[i][m]) && (i != l) && (pole[l][m] != ' ') && (pole[i][m] != ' ') ) { printf("Nespravny vstup.\n"); return 1; }
}
}
}
i = 0;
l = 0;
j = 0;
for (l = 0; l < 16; l+=3) {
for (i = 0; i < 4 ; i++) {
for (j = 0; j < 4; j++) {
for (x = 0; x < 4; x++) {
for (y = 0; y < 4; y++) {
if(i==x && j==y) continue;
if ( (pole[i][j] == pole[x][y]) && (pole[i][j] != ' ') && (pole[x][y] != ' ') ) { printf("Nespravny vstup.\n"); return 1; }
}
}
}
}
i+=3;
j+=3;
x+=3;
y+=3;
}

int hexadoku[16][16];
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
if(pole[i][j] == ' ') {
hexadoku[i][j] = -1;
} else {
hexadoku[i][j] = (int) (pole[i][j]-'a');
}
}
}
Hexadoka* h = new Hexadoka(hexadoku); //initialize problem
DFS<Hexadoka> e(h); //depth first search
delete h;
while (Hexadoka* s = e.next()) { //iterate over solutions
s->print(); delete s; //print the solution
}
}

Gecode 使用传播器进行(非常)快速的传播,并在必须做出决定时使用分支器。如果有多个解决方案,则打印所有解决方案。

我冒昧地重写了您代码的一些小部分,例如解析函数:

if(pole[i][j] == ' ') {
hexadoku[i][j] = -1;
} else {
hexadoku[i][j] = (int) (pole[i][j]-'a');
}

-1 用于表示未知值。否则使用 ASCII 转换。

当使用 perf 对程序(包括 I/O)进行基准测试时,它需要求解器:

 Performance counter stats for './Hexadoka':

10.646271 task-clock (msec) # 0.921 CPUs utilized
70 context-switches # 0.007 M/sec
0 cpu-migrations # 0.000 K/sec
1,806 page-faults # 0.170 M/sec
13,366,054 cycles # 1.255 GHz [39.33%]
8,786,841 stalled-cycles-frontend # 65.74% frontend cycles idle
<not supported> stalled-cycles-backend
15,827,908 instructions # 1.18 insns per cycle
# 0.56 stalled cycles per insn
3,186,179 branches # 299.277 M/sec
78,708 branch-misses # 2.47% of all branches
<not supported> L1-dcache-loads:HG
465,134 L1-dcache-load-misses:HG # 0.00% of all L1-dcache hits [63.40%]
99,919 LLC-loads:HG # 9.385 M/sec [26.66%]
<not supported> LLC-load-misses:HG

0.011555631 seconds time elapsed

不到 0.02 秒。

关于c - 加速 C 中的 hexadoku(16 x 16,a - p),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27722513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com