名字随便取吧 2013-10-19 07:31 采纳率: 0%
浏览 2609
已采纳

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条回答 默认 最新

  • 普通网友 2013-10-19 14:44
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    阿俊007 2022-06-08 23:38

    666

    回复
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部