2 u011404046 u011404046 于 2013.10.19 23:31 提问

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;
}

1个回答

lym753024200
lym753024200   2013.10.20 06:44
已采纳

逐语句地调试跟踪来检查死循环,你这段代码里面一些符号都不知道什么意思,也不注释下,我看一个局部代码看不懂。再就是C的函数头bool solution(Move move, Pos &beginPos)里面怎么会有&,引用在C++中才有的,你是不是把*错写成&了?

u011404046
u011404046 在VC里编的··就当做C++来看吧··调试过了··没发现问题在哪··你可以忽略printf()和count这两者··move是一个Stack,Pos是坐标结构体
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!