c++:跳马问题。在5*5的棋盘上,有一个国家象棋的马,从(1,1)点出发
c++源代码!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
c++源代码!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
展开
展开全部
int Num[25];
int N = 0;
int F = 0;
int visited(int n)
{
int i;
for (i = 0; i < N; i++)
{
if (n == Num[i])
{
return 1;
}
}
return 0;
}
void pt()
{
int a[5][5];
int i, j;
for (i = 0; i < 25; i++)
{
int m = Num[i];
a[m/5][m%5] = i+1;
}
printf("\n");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("\n");
}
int valid(int x, int y)
{
if (x >= 0 && x < 5 &&
y >= 0 && y < 5)
{
return 1;
}
return 0;
}
int jump(int x, int y)
{
static dir[8][2] = {
{1, 2}, {2, 1},
{-1, -2}, {-2, -1},
{1, -2}, {-2, 1},
{2, -1}, {-1, 2}
};
int i;
if (valid(x, y) &&
!visited(x*5+y))
{
Num[N] = x*5 + y;
if (N == 24)
{
F++;
if (F <= 5)
{
pt();
}
return 1;
}
N++;
}
else
{
return -1;
}
for (i = 0; i < 8; i++)
{
int mx, my;
mx = x + dir[i][0];
my = y + dir[i][1];
jump(mx, my);
}
N--;
}
int main()
{
jump(0, 0);
printf("Total find:%d\n", F);
getch();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询