- 论坛徽章:
- 0
|
关于马踏棋盘问题的求教
谢谢大家帮忙,我也些了个程序,通过限制递归的次数来解决这个问题,在这里我限制的递归次数为100000,我还想知道知道如果求解6*6或者8*8棋盘可能的解的个数,
这是我的程序,写的比较烂,请大家指教
/* 要求:
1.依次输出所走过的各位置的坐标。
2.画出棋盘的图形形式,并在其上动态的标注行走过程。
3.程序能方便的移植到其他规格的棋盘上。*/
/*Analyse:
1.init chessboard
2.start from (0.0) and try from direction 0
3.if can_go,go next;if can't,try another direction
4.if have try all the direction, go back old one and init the current point
5.if try all the direction of start point(0.0),return can't find, otherwise print the chessboard*/
#include <time.h>;
#define xSize 8
#define ySize 8
#define MAXSTEP 100000
int iStep = 0;
long allStep = 0;
int xRecord = 0,yRecord = 0;
int chessboard[xSize][ySize][2];//define chessboard;[0]--count,[1]--direction
int init_chess()//init chessboard
{
int i,j;
for(i=0;i<xSize;i++)
for(j=0;j<ySize;j++)
{
chessboard[j][0]=0;//init count
chessboard[j][1]=0;//init direction
}
}
int print_chess()//print chessboard
{
int i,j;
for(i=0;i<xSize;i++)
{
for(j=0;j<ySize;j++)
printf("%4d",chessboard[j][0]);
printf("\n\n" ;
}
}
int search_x(int count)//search xPoint by count
{
int i,j;
for(i=0;i<xSize;i++)
for(j=0;j<ySize;j++)
if(chessboard[j][0]==count)
return i;
return 0;
}
int search_y(int count)//search yPoint by count
{
int i,j;
if(count==0)
return 0;
for(i=0;i<xSize;i++)
for(j=0;j<ySize;j++)
if(chessboard[j][0]==count)
return j;
return 0;
}
int can_go(int m,int n)//
{
/*find if it out board or have been gone*/
if((m>;=0&&m<xSize&&n>;=0&&n<ySize)&&(chessboard[m][n][0] == 0))
return 1;//can go
else
return 0;//can't go
}
int go_back(int i,int j)
{
int m,n;
//find old one(m,n)
m = search_x(chessboard[j][0]-1);
n = search_y(chessboard[j][0]-1);
//(m,n) direction++
++chessboard[m][n][1];
//clear(i,j)
chessboard[j][0] = 0;
chessboard[j][1] = 0;
//go from(m,n)
//printf("go back to i=%d,j=%d,count=%d\n",m,n,chessboard[m][n][0]);
go(m,n);
}
int go(int i,int j)
{
int level = 0 , count = 0 ;
int m,n;
level = chessboard[j][1];
count = chessboard[j][0];
if(count == (xSize*ySize)-1)
{
iStep = -1;
return;
}
iStep++;
allStep++;
if(iStep >; MAXSTEP)
{
xRecord = i;
yRecord = j;
iStep = 0;
return;
}
switch(level)
{
case 0:
m = i-2;
n = j+1;
break;
case 1:
m = i-1;
n = j+2;
break;
case 2:
m = i+1;
n = j+2;
break;
case 3:
m = i+2;
n = j+1;
break;
case 4:
m = i+2;
n = j-1;
break;
case 5:
m = i+1;
n = j-2;
break;
case 6:
m = i-1;
n = j-2;
break;
case 7:
m = i-2;
n = j-1;
break;
default:/*if can't find next go to back to first.*/
//if(i==0&&j==0)/*search all but can't find*/
//{
// printf("Can't find any trace" ;
// return;
//}
go_back(i,j);
return;
}
if(can_go(m,n) == 1)
{
chessboard[m][n][0] = ++count;
//printf("can go.allStep=%d,iStep=%d,i=%d,j=%d,level=%d,count=%d\n",allStep,iStep,i,j,chessboard[j][1],chessboard[j][0]);
go(m,n);
}
else
{
chessboard[j][1] = ++level;
//printf("can't go.allStep=%diStep=%d,i=%d,j=%d,level=%d,count=%d\n",allStep,iStep,i,j,chessboard[j][1],chessboard[j][0]);
go(i,j);
}
}
//#define CLOCKS_PER_SEC 1000000
int main()
{
int i;
clock_t time;
//printf("time=%d",time);
//Init chessboard
init_chess();
//go from (0.0)
//go(0,0);
do
{
go(xRecord,yRecord);
}while(iStep != -1);
time = clock();
printf("Search OK,allStep=%d\n,time=%d\n",allStep,time/CLOCKS_PER_SEC);
//print go trace
print_chess();
} |
|