在C中查找组合的最有效方法 #包括 #包括 国际棋盘[8][8]; int指示符,x,i,j,b,校验和,testerint,temp,row,column; int重计数器,resstarter; void togglecolumn(int columnumber){ // 对于(j=0;j
我试图解决这个问题:“在计算机屏幕上有一个8x8表,所有的方块都被涂成白色。在每一步中,你都会选择任何方块,因此同一行和列上的所有方块(包括所选方块本身)都会切换颜色(白色变为黑色,黑色变为白色)。 获得标准彩色棋盘所需的最小步骤数是多少?” 为此,我将棋盘分成64块(8x8),并计算从1到64的所有64s组合。(我知道答案在1到64之间) 我的方法是从头到尾(棋盘)一直到全白。因此,我用1(黑色)和0(白色)填充棋盘,并在函数fillchessboard()中成功构建了棋盘。我可以很好地切换行和列,我选择的初始正方形是打开的 如果所有电路板均为白色,则检查方法为checkboard()。如果所有电路板均为白色,则此函数将指示器返回为0,否则返回1。我从小组合到大组合开始,每一步都检查电路板。因此,当指示器第一次返回为0时,它将是使电路板变为白色并成为问题答案的最小迭代数在C中查找组合的最有效方法 #包括 #包括 国际棋盘[8][8]; int指示符,x,i,j,b,校验和,testerint,temp,row,column; int重计数器,resstarter; void togglecolumn(int columnumber){ // 对于(j=0;j,c,recursion,C,Recursion,我试图解决这个问题:“在计算机屏幕上有一个8x8表,所有的方块都被涂成白色。在每一步中,你都会选择任何方块,因此同一行和列上的所有方块(包括所选方块本身)都会切换颜色(白色变为黑色,黑色变为白色)。 获得标准彩色棋盘所需的最小步骤数是多少?” 为此,我将棋盘分成64块(8x8),并计算从1到64的所有64s组合。(我知道答案在1到64之间) 我的方法是从头到尾(棋盘)一直到全白。因此,我用1(黑色)和0(白色)填充棋盘,并在函数fillchessboard()中成功构建了棋盘。我可以很好地切换行
到目前为止,我的代码工作正常,在10小时内就可以升级到第10次迭代。然而,这将需要越来越多的时间,所以第11次迭代将需要大约10个小时,第12次迭代将需要20个小时,以此类推。。。我的问题是,有没有什么方法能让这些指令更快速、更有效?我等不及一个星期才能解决这个问题。我将感谢任何帮助。谢谢 这不是一个解决方案,而是一些有助于降低问题复杂性(但不是难度)的指针 棋盘上有2^64个不同的状态 国际象棋棋盘的一些特性可以帮助你减少有趣状态的数量 每一步翻转15个瓷砖(奇数)。由于开始和结束时白瓷砖的数量为偶数,因此您知道移动的总数为偶数 此外,执行移动的顺序也不相关。选择同一个正方形两次将反转上一次翻转。因此,我们只需要找出64个瓷砖中要选择的瓷砖 因此,我们可以使用64位来表示解决方案,每个位表示选定的磁贴或未选定的磁贴。我们可以使用64位长来存储可能的
#include <stdio.h>
#include <stdlib.h>
int chessboard[8][8];
int indicator, x, i, j, b, checksum, testerint, temp, row, column;
int rescounter, resstarter;
void togglecolumn(int columnumber) {
//
for (j = 0; j < 8; j++) {
//
chessboard[j][columnumber] = toggleint(chessboard[j][columnumber]);
}
}
void togglerow(int rownumber) {
//
for (j = 0; j < 8; j++) {
//
chessboard[rownumber][j] = toggleint(chessboard[rownumber][j]);
}
}
void fulltoggle(int i, int j) {
//
togglerow(i);
togglecolumn(j);
chessboard[i][j] = toggleint(chessboard[i][j]);
}
int toggleint(int a) {
//
if (a == 0) {
b = 1;
}
if (a == 1) {
b = 0;
}
return b;
}
void fillchessboard() {
x = 1;
//
for (i = 0; i < 8; i++) {
x = toggleint(x);
for (j = 0; j < 8; j++) {
//
chessboard[i][j] = x;
x = toggleint(x);
}
}
}
void showchessboard() {
//
printf("------------------- \n \n");
for (i = 0; i < 8; i++) {
//
for (j = 0; j < 8; j++) {
//
if (j == 7) {
//if end of the row
printf("%d \n", chessboard[i][j]);
} else {
//
printf("%d ", chessboard[i][j]);
}
}
}
printf("\n \n");
printf("------------------- \n \n");
}
int checkboard() {
checksum = 0;
for (i = 0; i < 8; i++) {
//
for (j = 0; j < 8; j++) {
//
if (chessboard[i][j] == 1) {
//
return 1;
}
}
}
return 0;
}
void rowcolindicator(int i) {
//
if (i % 8 == 0) {
column = 7;
row = i / 8 - 1;
} else {
row = i / 8;
column = i % 8 - 1;
}
}
// for proper operation i should be chosen 0
int recurfuntion(int i, int stepcounter) {
if (stepcounter != 0) {
stepcounter--;
temp = i;
for (i = temp + 1; i < 65; i++) {
//do row and column for
rowcolindicator(i);
fulltoggle(row, column);
recurfuntion(i, stepcounter);
}
if (i == 65) {
i = temp++;
rowcolindicator(temp);
fulltoggle(row, column);
stepcounter++;
}
} else {
//
temp = i;
for (i = temp + 1; i < 65; i++) {
//do row and column for i code and return iteration number if board turns all right
rowcolindicator(i);
fulltoggle(row, column);
if (checkboard() == 0) {
//
showchessboard();
return 1;
} else {
//
fulltoggle(row, column);
}
}
if (i == 65) {
i = temp++;
rowcolindicator(temp);
fulltoggle(row, column);
stepcounter++;
//showchessboard();
}
}
}
int main(int argc, char *argv[]) {
fillchessboard();
showchessboard();
indicator = checkboard();
printf("indicator is %d \n", indicator);
for (rescounter = 0; rescounter < 1000; rescounter++) {
fillchessboard();
printf("iiteration number: %d \n", rescounter);
if (recurfuntion(0, rescounter) == 1) {
printf("iteration number is %d so is the answer :) \n", rescounter);
}
}
system("PAUSE");
return 0;
}