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.
2
01011
110 1
01110
01010
00100
10110
01 11
10111
01001
00000
Unsolvable in less than 11 move(s).
Solvable in 7 move(s).
题目大意:给出一个5*5的棋盘,求出至少要多少步才可以恢复到初始状态,如果大于10步就输出"Unsolvable in less than 11 move(s).",否则输出步数。
解析:这题的思路是bfs+hash,bfs用于枚举出所有的状态,hash用于判重。
八数码问题类似,基本照着刘汝佳的书上写,
#include <cstdio> #include <cstring> #include <set> using namespace std; const int N = 5; const int dx[]={-2,-2,-1,-1, 1, 1, 2,2}; const int dy[]={-1, 1,-2, 2,-2, 2,-1,1}; const int MAXSTATE = 1000000; const int goal[] ={ 1,1,1,1,1, 2,1,1,1,1, 2,2,0,1,1, 2,2,2,2,1, 2,2,2,2,2}; typedef int State[N*N]; State st[MAXSTATE]; int dist[MAXSTATE]; set<int> vis; inline void init_lookup_table() { vis.clear(); } inline int try_to_insert(int s) { int v = 0; for(int i = 0; i < 25; i++) { v = v*2 + st[s][i]; } if(vis.count(v)) { return 0; } vis.insert(v); return 1; } inline int bfs() { memset(dist,0,sizeof(dist)); init_lookup_table(); int front = 1,rear = 2; while(front < rear) { State& s = st[front]; if(!memcmp(goal,s,sizeof(s))) { return front; } if(dist[front] > 10) { return 0; } int z; for(z = 0; z < 25; z++) { if(!s[z]) { break; } } int x = z/5 , y = z%5; for(int d = 0; d < 8; d++) { int newx = x + dx[d]; int newy = y + dy[d]; int newz = newx * 5 + newy; if(newx < 0 || newx >= 5 || newy < 0 || newy >= 5) { continue; } State& t = st[rear]; memcpy(&t,&s,sizeof(s)); t[newz] = s[z]; t[z] = s[newz]; dist[rear] = dist[front] + 1; if(try_to_insert(rear)) { rear++; } } front++; } return 0; } int main() { int t; char tmp[10]; while(scanf("%d",&t) != EOF) { getchar(); while(t--) { for(int i = 0; i < 5; i++) { gets(tmp); for(int j = 0; j < 5; j++) { if(tmp[j] == '0') { st[1][i*5+j] = 2; }else if(tmp[j] == '1') { st[1][i*5+j] = 1; }else if(tmp[j] == ' ') { st[1][i*5+j] = 0; } } } int ans = bfs(); if(ans > 0) { printf("Solvable in %d move(s).\n",dist[ans]); }else { printf("Unsolvable in less than 11 move(s).\n"); } } } return 0; }