題目很簡單呀!!!
在國際象棋的棋盤上,將馬隨意放置,之后走日字,走完即可。
要求:8×8的棋盤
遍歷算法:
可以說是碰運氣,當你確定在某一位置時,根據規則你自然有了八種選擇,
2 | 3 | ||||||
1 | 4 | ||||||
H | |||||||
8 | 6 | ||||||
7 | 5 | ||||||
分別是
X= {i-2, i-1, i+1, i+2, i+2, i+1, i-1, i-2};
Y= {i-1, i-2, i-2, i-1, i+1, i+2, i+2, i+1};
往簡單了想,當其越界或是已被占有,下一個位置的可能去向就會減少,你要做的就是檢驗當前步的下一步的情況中有那個是可以走,那個不可以,可以了就直接往下走,不行就跳到下一步。但注意當八種情況都不行時就必須可以回溯到上一步的位置
(重點在於獲得上一步往下一步走選路情況,以便你不會再走同樣的路)。幸運的話,也許能出來,不幸的話,就和我的同學一樣,跑了三個小時還是沒出結果。
使用的數據結構是三個整型棧,分別存放x,y,t(當前步如何走向下一步)
注意以下代碼只能跑出(2,3)的位置,其他位置不是待檢驗,就是跑時間太長
人如果你想檢驗正確與否,就將代碼中宏定義的64,8改為36,6(其他也可以吧),檢驗之后是能跑的
1 /************************************************************************* 2 > File Name: mataqipan.c 3 > Author: zh 4 > Mail: 574932286@qq.com 5 > Created Time: 2014年09月28日 星期日 16時21分10秒 6 > 實現馬踏棋盤 7 8 ************************************************************************/ 9 #include<stdio.h> 10 #include<stdlib.h> 11 #define STACKSIZE 64 12 #define MAPSIZE 8 // 13 #define FALSE 0 14 #define TURE 1 15 16 typedef struct stack * Stack; 17 typedef int ElementType; 18 19 int IsEmpty( Stack S ); 20 int IsFull( Stack S ); 21 Stack CreateStack( void ); 22 int Push( Stack S, ElementType X ); 23 ElementType Pop( Stack S ); 24 ElementType Top( Stack S ); 25 int IntTest( int i, int j ); 26 int Test( int i, int j, int Map[][MAPSIZE] ); 27 void Hua( int i, int j, int Map[][MAPSIZE] ); 28 void ReHua( int i, int j, int Map[][MAPSIZE] ); 29 30 31 struct stack{ 32 int top; 33 ElementType Array[STACKSIZE]; 34 }; 35 36 int IsEmpty( Stack Q ) 37 { 38 return Q->top == -1; 39 }/*IsEmpty*/ 40 41 int IsFull( Stack Q ) 42 { 43 return Q->top == STACKSIZE-1; 44 }/*IsFull*/ 45 46 /*創建一個存放64個整型數的棧*/ 47 Stack CreateStack( void ) 48 { 49 Stack S; 50 S = (Stack)malloc(sizeof(struct stack)); 51 52 if( S == NULL ) 53 { 54 exit(1); 55 } 56 S->top = -1; 57 return S; 58 }/*CreateStack*/ 59 60 int Push( Stack S, ElementType X ) 61 { 62 if( IsFull(S) ) 63 { 64 return FALSE; 65 } 66 else 67 { 68 S->top++; 69 S->Array[S->top] = X; 70 return TURE; 71 } 72 }/* Push */ 73 74 /* 出棧並且返回出棧的值 */ 75 ElementType Pop( Stack S ) 76 { 77 int temp; 78 if( IsEmpty(S) ) 79 { 80 printf("棧空\n"); 81 getchar(); 82 exit(0); 83 } 84 else 85 { 86 temp=S->top; 87 S->top--; 88 return S->Array[temp]; 89 } 90 }/* Pop */ 91 92 /* 輸出棧頂元素 */ 93 ElementType Top( Stack S ) 94 { 95 if( IsEmpty(S) ) 96 { 97 printf("棧空\n"); 98 system("PAUSE"); 99 exit(0); 100 } 101 else 102 { 103 return S->Array[S->top]; 104 } 105 }/* Top */ 106 107 /* 檢驗被初始化的i,j 是否越位,沒有越位返回TURE,否則返回FLASE */ 108 int IntTest( int i, int j ) 109 { 110 if( i>=0 && i<MAPSIZE && j>=0 && j<MAPSIZE ) 111 { 112 return TURE; 113 } 114 else 115 { 116 return FALSE; 117 } 118 }/* IntTest */ 119 120 /* 判斷該位置是否已經被占和該位置是否越位,符合條件返回TURE ,否則返回FLASE*/ 121 int Test( int i, int j, int Map[][MAPSIZE] ) 122 { 123 if( Map[i][j]==1 || !IntTest(i,j) )//判斷該位置是否已經被占和該位置是否越位 124 { 125 return FALSE; 126 } 127 else 128 { 129 return TURE; 130 } 131 }/* Test */ 132 133 /* 為MAP做標記 */ 134 void Hua( int i, int j, int Map[][MAPSIZE] ) 135 { 136 Map[i][j] = TURE; 137 }/* Hua */ 138 139 /* 消除Map上的標記 */ 140 void ReHua( int i, int j, int Map[][MAPSIZE] ) 141 { 142 Map[i][j] = FALSE; 143 }/* ReHUa */ 144 145 int main( void ) 146 { 147 Stack A, B, C ;/*3個棧分別用來存儲 i,j.t[下一步棋在當前步棋八個位置中的那一個]*/ 148 int Map[MAPSIZE][MAPSIZE]={0}; 149 int a[8]={-2,-1, 1, 2,2,1,-1,-2}; 150 int b[8]={-1,-2,-2,-1,1,2, 2, 1}; 151 int i,j,t; 152 int temp; 153 154 A = CreateStack(); 155 B = CreateStack(); 156 C = CreateStack();//棧的初始化 157 do{ 158 printf("輸入x :"); 159 scanf("%d",&i); 160 printf("輸入y :"); 161 scanf("%d",&j); 162 temp=IntTest( i, j );//測試輸入 163 }while(!temp ); 164 165 Hua( i, j, Map); 166 Push( A, i ); 167 Push( B, j ); 168 Push( C, -1 ); 169 170 while( !IsFull(A) ) 171 { 172 173 if( IsEmpty( A ) ) 174 { printf("Oh No, why ,shit ,fuck,come up \n"); 175 printf(" 不可能有這種走法\n"); 176 getchar(); 177 exit(0); 178 } 179 //棧空的可能性 180 181 i = Top( A ); 182 j = Top( B ); 183 t = Top( C )+1; 184 185 while( t<=8-1 && !Test( a[t]+i, b[t]+j , Map ) ) // 186 { 187 t++; 188 if( t == 8 ) 189 break; 190 }//while,用以判斷8種可能那種不合適 191 192 if( t == 8 ) 193 { 194 i = Pop( A ); 195 j = Pop( B ); 196 Pop( C ); 197 ReHua( i, j, Map ); 198 }//回溯到上一步 199 else 200 { 201 Push( A, a[t]+i ); 202 Push( B, b[t]+j ); 203 Pop( C ); 204 Push( C, t ); 205 Push( C, -1 ); 206 Hua( a[t]+i, b[t]+j, Map ); 207 }// 下一步被確認,將i,j,t 放入棧中,並將-1放入棧,在map上標記該步 208 209 } 210 printf("OK\n"); 211 getchar(); 212 for( i=0; i<MAPSIZE; i++ ) 213 for( j=0; j<MAPSIZE; j++ ) 214 { 215 Map[A->Array[i*MAPSIZE+j]][B->Array[i*MAPSIZE+j]]=i*MAPSIZE+j; 216 } 217 for( i=0; i<MAPSIZE; i++ ) 218 for( j=0; j<MAPSIZE; j++ ) 219 { 220 printf("%5d",Map[i][j]); 221 if( j == MAPSIZE-1 ) 222 printf("\n"); 223 } 224 getchar(); 225 return 0; 226 }