- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的六边形求解器有严重问题。我想在 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/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!