关于骑士周游(马踏棋盘)的问题
本帖最后由 蘼芜 于 2019-2-11 22:30 编辑#include<stdio.h>
#include<time.h>
#define X 5
#define Y 5
void copychess(int nowchess, int chess)//复制棋盘
{
int i, j;
for (i = 0; i < X; i++)
{
for (j = 0; j < Y; j++)
{
nowchess = chess;
}
}
}
void printchess(int chess)
{
int i, j;
for (i = 0; i < X; i++)
{
for (j = 0; j < Y; j++)
{
printf("%02d ", chess);
}
putchar('\n');
}
printf("****************************************************************************************************\n\n");
}
void travel(int chess, int x, int y,int count)
{
int nowchess;
copychess(nowchess, chess);
int i, j;
//调试
//printf("第%d次递归", count);
if (count > X*Y)
{
printchess(chess);
}
else
{
nowchess = count;
for (i = 0; i < X; i++)
{
for (j = 0; j < Y; j++)
{
if (nowchess == 0)
{
if ((i - x)*(i - x) == 4 && (j - y)*(j - y) == 1)
{
travel(nowchess, i, j, count + 1);
}
else if ((i - x)*(i - x) == 1 && (j - y)*(j - y) == 4)
{
travel(nowchess, i, j, count + 1);
}
}
}
}
}
}
int main(void)
{
clock_t start, end;
int x, y;
int chess = { 0 };
printchess(chess);
//scanf_s("%d,%d", &x, &y);
start = clock();
for (x = 0; x < X; x++)
{
for (y = 0; y < Y; y++)
{
travel(chess, x, y, 1);
}
}
//travel(chess, x, y, 1);
end = clock();
printf("The program has run: %f s.\n", (double)((end - start) / CLOCKS_PER_SEC));
getchar();
getchar();
return 0;
}
算是纯递归的,8*8的单点好久算不出,认了,5*5的算全部解法出错(无解),想请教下这是哪里出问题了
{:10_269:}
页:
[1]