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).
(Problem Setter: Piotr Rudnicki, University of Alberta, Canada)
解题思路:我把25位压成了三进制数,用map记录有没有出现过,BFS一遍。
三进制数的操作有技巧 对于一个三进制数num=a0*3^0+a1*3^1+a2*3^2+a3*3^3.....
第x位上的数字是num/3^x%3,n进制 也同理。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#define mod 107
#define ll long long
using namespace std;
int t;
struct Map{
int x,y,l;
ll st;
};
char str[6][6];
ll ST;
set<ll> vis;
ll g[26];
int d[8][2]={-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2};
void init(){
g[0]=1;
for(int i=1;i<26;i++) g[i]=g[i-1]*3;
vis.clear();
}
int sx,sy;
void read(){
ST=0;
scanf("\n");
for(int i=0;i<5;i++){
gets(str[i]);
for(int j=0;j<5;j++){
if(str[i][j]==' ') sx=i,sy=j;
if(str[i][j]==' ') ST=ST*3+2;
else if(str[i][j]=='0') ST=ST*3;
else ST=ST*3+1;
}
}
vis.insert(ST);
}
int calc(int x,int y){return (4-x)*5+(4-y);}
void solve(){
queue<Map> q;
Map f;
f.x=sx,f.y=sy,f.l=0,f.st=ST;
q.push(f);
int ret=-1;
while(!q.empty()){
Map z=q.front();
q.pop();
if(z.st==422476168122L){
ret=z.l;
break;
}
if(z.l==10) continue;
for(int i=0;i<8;i++){
f.x=z.x+d[i][0];
f.y=z.y+d[i][1];
if(!(f.x>=0&&f.x<5&&f.y>=0&&f.y<5)) continue;
int s=calc(f.x,f.y),ss=calc(z.x,z.y);
int tmp1=z.st/g[s]%3,tmp2=z.st/g[ss]%3;
f.st=z.st;
f.st-=tmp1*g[s];
f.st+=tmp2*g[s];
f.st-=tmp2*g[ss];
f.st+=tmp1*g[ss];
f.l=z.l+1;
if(vis.find(f.st)==vis.end()){
vis.insert(f.st);
q.push(f);
}
}
}
if(ret==-1) puts("Unsolvable in less than 11 move(s).");
else printf("Solvable in %d move(s).\n",ret);
}
int main(){
scanf("%d",&t);
for(int ca=1;ca<=t;ca++){
init();
read();
solve();
}
return 0;
}