C语言版非递归马踏棋盘·死循环了·求大神解答·小弟新手求助


这是出现死循环的代码

   
  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 数据结构

有知识长头发 11 years, 4 months ago

Your Answer