/*對馬踏棋盤的一點研究*/ /* 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數組中對相應點賦值*/