8皇后带回溯C ++ [英] 8 Queens With Backtracking C++
本文介绍了8皇后带回溯C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对此进行了编码,但似乎在某个地方陷入了无限循环而且根本没有输出任何东西。
任何帮助都会非常感激。< br $> b $ b
I coded this but it seems to be stuck in an infinite loop somewhere and isn't outputting anything at all.
Any help would be much appreciated.
int board[8][8] = {0}, r =0, c=0, count =0;
board[0][0] = 1;
NC: c++;
if(c==8){
goto PRINT;
}
r =-1;
NR: r++;
if(r==8)
goto BT;
for(int i=0; iif(board[r][i] == 1)
goto NR;
}
for(int i=1; (r-i)>=0 && (c-i)>=0; i++){//Up-diagonal test
if(board[r-i][c-i] == 1)
goto NR;
}
for(int i =1; (r+i)<=8 && (c-i)>=0; i++){//Down-diagonal test
if(board[r+i][c-i] == 1)
goto NR;
else{
board[r][c] =1;
goto NC;
}
}
BT: c--;
if(c == -1)
return 0;
r=0;
while(board[r][c] != 1){//Search the column until a queen is found
r++;
board[r][c] = 1;//Once a queen is found, take it away and continue the search
goto NR;//Stay in the column and look for more options
}
PRINT:
// void print(int board[][8]){
// static
cout<< "Solution #"<< count++<< ":"<< endl;
for(int i =0;i<8; i++){
for(int j =0; j<8; j++){
cout<< board[i][j];
}
cout<< endl;
}
goto BT;
// }
}
推荐答案
另请参阅:
使用遗传算法的N-Queen问题 [ ^ ]
8Queen问题 [ ^ ]
8个女王问题的图形解决方案 [ ^ ]
使用遗传算法的Queens解决方案 [ ^ ]
Q Queen Solution New- VB.NET [ ^ ]
但是,严肃地说,有比你正在做的更有效的方式。
See also:
N-Queen Problem using Genetic Algorithms[^]
8Queen Problem[^]
Graphical solution to eight queen problem[^]
8 Queens Solution with Genetic Algorithm[^]
Q Queen Solution New- VB.NET[^]
But, seriously, there are more efficient ways than what you are doing.
这篇关于8皇后带回溯C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文