uva10422 - Knights in FEN
利用set 进行判断重复, 可以用dfs 因为只有11步。 我这里用了bfs
题目:
Problem D
Knights in FEN
Input: standard input
Output: standard output
Time Limit: 10 seconds
There are black and white knights on a 5 by 5 chessboard. There are twelve of each color, and there is one square that is empty. At any time, a knight can move into an empty square as long as it moves like a knight in normal chess (what else did you expect?).
Given an initial position of the board, the question is: what is the minimum number of moves in which we can reach the final position which is:
Input
First line of the input file contains an integer N (N<14) that indicates how many sets of inputs are there. The description of each set is given below:
Each set consists of five lines; each line represents one row of a chessboard. The positions occupied by white knights are marked by 0 and the positions occupied by black knights are marked by 1. The space corresponds to the empty square on board.
There is no blank line between the two sets of input.
The first set of the sample input below corresponds to this configuration:
Output
For each set your task is to find the minimum number of moves leading from the starting input configuration to the final one. If that number is bigger than 10, then output one line stating
Unsolvable in less than 11 move(s).
otherwise output one line stating
Solvable in n move(s).
where n <= 10.
The output for each set is produced in a single line as shown in the sample output.
Sample Input
2
01011
110 1
01110
01010
00100
10110
01 11
10111
01001
00000
Sample Output
Unsolvable in less than 11 move(s).
Solvable in 7 move(s).
代码:
1 #include <iostream> 2 #include <set> 3 #include <algorithm> 4 #include <memory.h> 5 #include <cstdio> 6 using namespace std; 7 8 9 typedef int State[25]; 10 const int maxn = 5000000; 11 State st [maxn]; 12 State goal = {1,1,1,1,1, 13 0,1,1,1,1, 14 0,0,2,1,1, 15 0,0,0,0,1, 16 0,0,0,0,0}; 17 int dis[maxn]; 18 int dx[]={-1,-2,-1,-2,1,2,1,2}; 19 int dy[]={-2,-1,2,1,-2,-1,2,1}; 20 21 22 struct cmp 23 { 24 bool operator ()(int a,int b)const 25 { 26 return memcmp(&st[a],&st[b],sizeof(st[b]))<0; 27 } 28 29 }; 30 31 set<int,cmp> vis; 32 int try_to_insert(int s) 33 { 34 if(vis.count(s))return 0; 35 vis.insert(s); 36 return 1; 37 } 38 39 int bfs() 40 { 41 vis.clear(); 42 43 int front=0,rear=1; 44 45 while(front<rear) 46 { 47 State& s = st[front]; 48 if(memcmp(goal,s,sizeof(s))==0) return front; 49 50 int z; 51 for( z=0;z<25;z++)if(s[z]==2)break; 52 53 int x = z/5,y=z%5; 54 55 for(int d=0;d<8;d++) 56 { 57 int newx = x+dx[d]; 58 int newy = y+dy[d]; 59 int newz = newx*5+newy; 60 61 if(newx>=0&&newx<5 && newy>=0&&newy<5) 62 { 63 // cout<<"Old "<<x<<" "<<y<<" "<<z<<endl; 64 // 65 // cout<<"New "<<newx<<" "<<newy<<" "<<newz<<endl; 66 State & t = st[rear]; 67 memcpy(&t,&s,sizeof(s)); 68 69 t[newz]=s[z]; 70 t[z]=s[newz]; 71 72 dis[rear]=dis[front]+1; 73 74 if(try_to_insert(rear)) 75 { 76 // for(int i=0;i<5;i++) 77 // { 78 // for(int j=0;j<5;j++) 79 // { 80 // cout<<st[rear][i*5+j]; 81 // } 82 // cout<<endl; 83 // } 84 rear++; 85 } 86 } 87 } 88 if(dis[front]>=11)return -1; 89 front++; 90 91 } 92 return -1; 93 94 } 95 96 int main() 97 { 98 int tst; 99 cin>>tst; 100 cin.ignore(); 101 while(tst--) 102 { 103 char c; 104 for(int i=0;i<5;i++) 105 { 106 for(int j=0;j<5;j++) 107 { 108 109 c=getchar(); 110 if(c==' ')st[0][i*5+j] = 2; 111 else 112 st[0][i*5+j] = c-'0'; 113 } 114 getchar(); 115 } 116 117 118 119 int ans; 120 ans = bfs(); 121 if(ans==-1)cout<<"Unsolvable in less than 11 move(s)."<<endl; 122 else 123 cout<<"Solvable in "<<dis[ans]<<" move(s)."<<endl; 124 125 //memset(dis,0,sizeof(dis)); 126 } 127 128 129 130 return 0; 131 }