关于递归的跳马问题。

dccrazyboy dccrazyboy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
题目如下:在5*5格的棋盘上,有一个中国象棋的马,它可以朝8个方向跳,但不允许出界或跳到已跳过的格子上,要求不重复的跳过所有位置。(最后不要求回到起点)
这是我写的代码,但有问题,在第一次递归时就提示错误:unhandled exception in cpp1.exe 0xC0000FD:Stack Overflow.

#include<iostream>
using namespace std;
#define size 5
int num=0; //用来统计一共有多少种;
void put(bool chess[size][size],int x,int y);
bool finish(bool chess[size][size]);
//bool chess[size][size] true表示已经走过了,false表示没有走过。
int main()
{ bool chess[size][size];
//初始化表格;
int i,j;
for (i = 0 ; i < size ; i++)
for (j = 0 ; j< size ; j++)
chess[i][j]=false;
//放置;
put(chess,0,0);
cout << num;
system("pause");
}

void put(bool chess[size][size],int x,int y)
{ int i,j;
chess[x][y]=true;
if ( !finish(chess) ) //没有结束;
for (i = 0 ; i < size ; i++)
for (j = 0 ; j< size ; j++)
{ if ( (x-i)*(x-i) + (y-j)*(y-j) == 5 ) //满足两点距离的平方为5,所以可以走;
put(chess,i,j);
}
else
num++;
}

bool finish(bool chess[size][size])
{ int i,j;
for (i = 0 ; i < size ; i++)
for (j = 0 ; j< size ; j++)
if ( chess[i][j] != true ) return false;
return true;
}


请问下是怎么回事?
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
dccrazyboy
C++ 语言 65,214 成员 250,514 内容