A * search actually only spaces then go
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6#include <ctime>7#include <algorithm>8 #defineRep (i,l,r) for (int i=l;i<r;i++)9 #defineCLR (a,x) memset (A,x,sizeof (a))Ten #defineLegal (x, y) (x<n&&0<=x&&y<n&&0<=y) One using namespacestd; A Const intn=5; - Const intg[n][n]={{1,1,1,1,1} -,{0,1,1,1,1} the,{0,0,2,1,1} -,{0,0,0,0,1} -,{0,0,0,0,0}}; - Const intdir[8][2]={{1,2},{2,1},{1,-2},{-2,1},{-1,2},{2,-1},{-1,-2},{-2,-1}}; + intNow[n][n],ans; - BOOLFlag; + BOOLSame () A { atRep (I,0, N) -Rep (J,0, N) - if(Now[i][j]!=g[i][j])return 0; - return 1; - } - BOOLEstimateintd) in { -Rep (J,0, N) toRep (K,0, N) + if(now[j][k]!=G[j][k]) - if(++d>ans)return 0; the return 1; * } $ intCnt=0;Panax Notoginseng voidDfsintXintYintd) - { the if(d==ans&&Same ()) { +flag=1; A return; the } + if(flag)return; -Rep (I,0,8){ $ intx1=x+dir[i][0],y1=y+dir[i][1]; $ if(Legal (x1,y1)) { - swap (now[x][y],now[x1][y1]); - if(Estimate (d)) DFS (x1,y1,d+1); the swap (now[x][y],now[x1][y1]); - }Wuyi } the } - intMain () Wu { - intT; AboutCin>>T; $ while(t--){ -CLR (now,0); - intx, y; -flag=0; ARep (I,0, N) +Rep (J,0, N) { the CharC=GetChar (); - while(c!='*'&&!isdigit (c)) c=GetChar (); $ if(c=='*') now[x=i][y=j]=2; the Elsenow[i][j]=c-'0'; the } the for(ans=1; ans<= the; ans++){ theDFS (x, Y,0); - if(flag) Break; in } theprintf"%d\n", flag?ans:-1); the } About return 0; the}
View Code 1085: [SCOI2005] Knight Spirit time limit:10 Sec Memory limit:162 MB
submit:1110 solved:602
[Submit] [Status] [Discuss] Description
On a 5x5 chessboard there are 12 white knights and 12 Black Knights, and there is a vacancy. At any time a knight can follow the Knight's Walk (it can go to and its horizontal axis difference is 1, the ordinate difference is 2 or the horizontal axis difference is 2, the ordinate difference is 1 lattice) moved to the vacancy. Given an initial chessboard, how can it be moved to become the following target board: In order to demonstrate chivalry, they must complete the task with a minimum number of steps.
Input
The first line has a positive integer T (t<=10), which represents a total of n sets of data. Next there is the T-5x5 matrix, 0 for the White Knight, 1 for the Black Knight, * for the empty space. There are no blank lines between the two sets of data.
Output
Output one row for each set of data. If you can reach the target State within 15 steps (including 15 steps), the output steps, otherwise output-1.
Sample Input2
10110
01*11
10111
01001
00000
01011
110*1
01110
01010
00100
Sample Output7
-1
HINT Source [Submit] [Status] [Discuss]
bzoj1085 Knight Spirit *