马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 蘼芜 于 2019-2-11 22:30 编辑 #include<stdio.h>
#include<time.h>
#define X 5
#define Y 5
void copychess(int nowchess[X][Y], int chess[X][Y])//复制棋盘
{
int i, j;
for (i = 0; i < X; i++)
{
for (j = 0; j < Y; j++)
{
nowchess[i][j] = chess[i][j];
}
}
}
void printchess(int chess[X][Y])
{
int i, j;
for (i = 0; i < X; i++)
{
for (j = 0; j < Y; j++)
{
printf("%02d ", chess[i][j]);
}
putchar('\n');
}
printf("****************************************************************************************************\n\n");
}
void travel(int chess[X][Y], int x, int y,int count)
{
int nowchess[X][Y];
copychess(nowchess, chess);
int i, j;
//调试
//printf("第%d次递归", count);
if (count > X*Y)
{
printchess(chess);
}
else
{
nowchess[x][y] = count;
for (i = 0; i < X; i++)
{
for (j = 0; j < Y; j++)
{
if (nowchess[i][j] == 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[X][Y] = { 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的算全部解法出错(无解),想请教下这是哪里出问题了
|