/*对马踏棋盘的一点研究*/ /* QQ:164094487 */ /* Email: fygood@163.com */ /*欢迎与我联系,讨论问题 */ /*本人先后编了两次,第二次进行了改进。改进的思想主要是注重到棋盘上每一点的下一可到达点的个数 (下称为权值)不同,对于可到达点较少(权值小)的点应该先跳上去,这样后来跳的点可跳的方向就比 较多,回溯的现象就比较少,这样就可以大幅度提高速度*//*第一次*/ /*原始的马踏棋盘,未加权值,有些点速度很慢*/#include "stdio.h" #define N 8 int w=0; int way1[8]={-2,-1,1,2, 2, 1,-1,-2}; int way2[8]={ 1,2, 2,1,-1,-2,-2,-1}; int ch[N*N]=; int a[N*N+1][3]=; int st=1; char c='y';void PRint() { int x,y;
printf(" ------%d answer---- ",++w);
for(x=1;x<N+1;x++) { printf(" "); for(y=1;y<N+1;y++) printf("%2d ",ch[(x-1)*N+y-1]); printf(" "); } printf(" Press n to quit ,press any other key to continue. "); c=getchar(); /*询问是否继续输出结果*/ }main() { int x,y,way; printf("Please enter the row and column of the starting point. "); scanf("%d,%d",&a[1][0],&a[1][1]);/*输入行数和列数*/ getchar(); /*接收回车符*/ x=a[1][0],y=a[1][1]; ch[(x-1)*N+y-1]=1; /*在ch数组中对相应点赋值*/
for(x=1;x<N+1;x++) { printf(" "); for(y=1;y<N+1;y++) printf("%2d ",ch[(x-1)*N+y-1]); printf(" "); } printf(" Press n to quit ,press any other key to continue. "); c=getchar(); /*询问是否继续输出结果*/ }main() { int x,y,way,way0; caculate(); directions(); printf("Please enter the row and column of the starting point. "); scanf("%d,%d",&a[1][0],&a[1][1]);/*输入行数和列数*/ getchar(); /*接收回车符*/ x=a[1][0],y=a[1][1]; ch[(x-1)*N+y-1]=1; /*在ch数组中对相应点赋值*/