#include<stdio.h>
int d_x[8] = { 2,2,1,1,-2,-2,-1,-1};
int d_y[8] = { 1,-1,2,-2,1,-1,2,-2};
//马行走的八个方向
int c_x = 1;
int c_y = 1;
//当前马的坐标位置
int o_x;
int o_y;
int o_i;
int path[26] = { 1 };
int step = 1; //当前步数
int num;
int flag = 0;
int map[5][5] = {
{ 1,2,3,4,5,},
{ 6,7,8,9,10, },
{ 11,12,13,14,15, },
{ 16,17,18,19,20, },
{ 21,22,23,24,25, },
};
int JC(int num);
void Display();
void main()
{
int i;
while (step > 0)
{
for (i = 0; i < 8; i++)
{
c_x = c_x + d_x[i];
c_y = c_y + d_y[i];
if (c_x < 6 && c_x > 0 && c_y < 6 && c_y > 0)//有效方向
{
num = map[c_x - 1][c_y - 1];
if (JC(num)) //符合条件
{
step++;
path[step] = num;
break;
}
}
}
if (i != 8)
{
if (step == 25)
{
Display();
}
else
{
step++;
path[step] = num;
}
}
else
{
step--; //应该返回下一个分支。 //应该返回下一个方向而不是下一步 用函数回不回好一点
}
}
}
void Display()
{
int i;
for (i = 1; i < 26; i++)
{
printf("Path %d : (%d, %d )\n", i, i, path[i]);
}
}
int JC(int num)
{
int i;
for (i = 0; i < step; i++)
{
if (num == path[i])
return 0;
}
return 1;
}
原文地址:https://www.cnblogs.com/lux-ace/p/5232103.html