程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c語言-C語言版非遞歸馬踏棋盤·死循環了·求大神解答·小弟新手求助

c語言-C語言版非遞歸馬踏棋盤·死循環了·求大神解答·小弟新手求助

編輯:編程綜合問答
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的函數頭bool solution(Move move, Pos &beginPos)裡面怎麼會有&,引用在C++中才有的,你是不是把*錯寫成&了?

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved