请教马跳棋盘的问题

[复制链接]
查看11 | 回复2 | 2021-1-27 06:48:00 | 显示全部楼层 |阅读模式
问题:在[8*8]的棋盘上,有一匹马随机放在棋盘上,按照马的斜日字走法,有什么办法可以不重复的走过棋盘所有的格子
我知道网上有很多例程,不过我想大家帮忙看看我的程序错在哪里,我调试了1天还是没有做出结果:
#defineDIM10
typedefstruct
{
intx;
inty;
}Position;
intcountstep=0;
intdirection[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};//马可能前进的8个方向
intm,n;
//递归方法,类似八皇后问题
intChevalierSearch2(intposx,intposy)
{
//以下两个语句不能放在for循环的if语句前面,因为m和n的取值可能会使mark[m][n]的值范围越界。所以我们对于具体问题要具体分析,不能完全按照模板来做。
countstep++;
mark[posx][posy]=countstep;
if(countstep==64)
{
printf("\nwefindasolution\n");
for(inti=1;i=1&&m=1&&n
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:48:00 | 显示全部楼层
晕死,上面的程序搞错了,以下面这个为准
#defineDIM10
typedefstruct
{
intx;
inty;
}Position;
intcountstep=0;
intdirection[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};//马可能前进的8个方向
intm,n;
//递归方法,类似八皇后问题
intChevalierSearch3(Positionpos)
{
countstep++;//对于当前点设置访问几号,其中mark[][]标示该节点是第几个被访问的点
mark[pos.x][pos.y]=countstep;
if(countstep==64)
{
printf("\nwefindasolution\n");
for(inti=1;i=1&&m=1&&n#include
usingnamespacestd;
#defineDIM10
typedefstruct
{
intx;
inty;
}Position;
intmark[DIM][DIM];
intcountstep=0;
boolfound=false;
intdirection[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};//马可能前进的8个方向
intm,n;
//递归方法,类似八皇后问题
intChevalierSearch3(Positionpos)
{
countstep++;//对于当前点设置访问几号,其中mark[][]标示该节点是第几个被访问的点
mark[pos.x][pos.y]=countstep;
if(countstep==64)
{
found=true;
printf("\nwefindasolution\n");
for(inti=1;i=1&&m=1&&n
回复

使用道具 举报

千问 | 2021-1-27 06:48:00 | 显示全部楼层
2楼的有一个问题,是不是如果找不到出口,就会永远执行下去,程序好像没有设置失败的出口。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行