這是出現死循環的代碼bool solution(Move move, Pos &beginPos){
if(!move) {
printf("solution Failed!");
return false;
}
int chessBoard[8][8] = {0};
push(move, beginPos);
chessBoard[beginPos.mX][beginPos.mY] = 1;
int steps = 1;
Pos nextMove;
Pos currentPos;
while(steps < 64) {
if(getTop(move, currentPos)) {
++count;
int x = currentPos.mX;
int y = currentPos.mY;
switch(currentPos.currentStep) {
case 0: if(x + 1 < 8 && y - 2 >= 0 && chessBoard[x + 1][y - 2] != 1) {
nextMove.mX = x + 1;
nextMove.mY = y - 2;
nextMove.currentStep = 0;
(move->mTop - 1)->currentStep = 0;
push(move, nextMove);
chessBoard[x + 1][y - 2] = 1;
++steps;
printf("steps=%d %d %d\n",steps, (move->mTop - 1)->mX, (move->mTop - 1)->mY);
break;
}
case 1: if(x + 2 < 8 && y - 1 >= 0 && chessBoard[x + 2][y - 1] != 1) {
nextMove.mX = x + 2;
nextMove.mY = y - 1;
nextMove.currentStep = 0;
(move->mTop - 1)->currentStep = 1;
push(move, nextMove);
chessBoard[x + 2][y - 1] = 1;
++steps;
printf("steps=%d %d %d\n",steps, (move->mTop - 1)->mX, (move->mTop - 1)->mY);
break;
}
case 2: if(x + 2 < 8 && y + 1 < 8 && chessBoard[x + 2][y + 1] != 1) {
nextMove.mX = x + 2;
nextMove.mY = y + 1;
nextMove.currentStep = 0;
(move->mTop - 1)->currentStep = 2;
push(move, nextMove);
chessBoard[x + 2][y + 1] = 1;
++steps;
printf("steps=%d %d %d\n",steps, (move->mTop - 1)->mX, (move->mTop - 1)->mY);
break;
}
case 3: if(x + 1 < 8 && y + 2 < 8 && chessBoard[x + 1][y + 2] != 1) {
nextMove.mX = x + 1;
nextMove.mY = y + 2;
nextMove.currentStep = 0;
(move->mTop - 1)->currentStep = 3;
push(move, nextMove);
chessBoard[x + 1][y + 2] = 1;
++steps;
printf("steps=%d %d %d\n",steps, (move->mTop - 1)->mX, (move->mTop - 1)->mY);
break;
}
case 4: if(x - 1 >= 0 && y + 2 < 8 && chessBoard[x - 1][y + 2] != 1) {
nextMove.mX = x - 1;
nextMove.mY = y + 2;
nextMove.currentStep = 0;
(move->mTop - 1)->currentStep = 4;
push(move, nextMove);
chessBoard[x - 1][y + 2] = 1;
++steps;
printf("steps=%d %d %d\n",steps, (move->mTop - 1)->mX, (move->mTop - 1)->mY);
break;
}
case 5: if(x - 2 >= 0 && y + 1 < 8 && chessBoard[x - 2][y + 1] != 1) {
nextMove.mX = x - 2;
nextMove.mY = y + 1;
nextMove.currentStep = 0;
(move->mTop - 1)->currentStep = 5;
push(move, nextMove);
chessBoard[x - 2][y + 1] = 1;
++steps;
printf("steps=%d %d %d\n",steps, (move->mTop - 1)->mX, (move->mTop - 1)->mY);
break;
}
case 6: if(x - 2 >= 0 && y - 1 >= 0 && chessBoard[x - 2][y - 1] != 1) {
nextMove.mX = x - 2;
nextMove.mY = y - 1;
nextMove.currentStep = 0;
(move->mTop - 1)->currentStep = 6;
push(move, nextMove);
chessBoard[x - 2][y - 1] = 1;
++steps;
printf("steps=%d %d %d\n",steps, (move->mTop - 1)->mX, (move->mTop - 1)->mY);
break;
}
case 7: if(x - 1 >= 0 && y - 2 >= 0 && chessBoard[x - 1][y - 2] != 1) {
nextMove.mX = x - 1;
nextMove.mY = y - 2;
nextMove.currentStep = 0;
(move->mTop - 1)->currentStep = 7;
push(move, nextMove);
chessBoard[x - 1][y - 2] = 1;
++steps;
printf("steps=%d %d %d\n",steps, (move->mTop - 1)->mX, (move->mTop - 1)->mY);
break;
}
default:{
printf("count=%d\n", count);
Pos lastMove;
pop(move, lastMove);
chessBoard[lastMove.mX][lastMove.mY] = 0;
--steps;
getTop(move, currentPos);
(move->mTop - 1)->currentStep += 1;
}
}
}
}
return true;
}
逐語句地調試跟蹤來檢查死循環,你這段代碼裡面一些符號都不知道什麼意思,也不注釋下,我看一個局部代碼看不懂。再就是C的函數頭bool solution(Move move, Pos &beginPos)裡面怎麼會有&,引用在C++中才有的,你是不是把*錯寫成&了?