Description
Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk at . A variation of it, called S-Dao, is a one-player game. In S-Dao, the game board is a * square with cells. There are black stones and white stones placed on the game board randomly in the beginning. The player is given a final position and asked to play the game using the following rules such that the final position is reached using the minimum number of moves:
. You first move a white stone, and then a black stone. You then alternatively move a white stone and a black stone. . A stone can be moved horizontally, vertically or diagonally. A stone must be moved in a direction until the boarder or another stone is encountered. There is no capture or jump. . During each move, you need to move a stone of the right color. You cannot pass. An example of a sequence of legal moves is shown in the following figure. This move sequence takes moves. This is not a sequence of legal moves
using the least number of moves assume the leftmost board is the initial position and the rightmost board is the final position. A sequence of moves using only moves is shown below.
Given an initial position and a final position, your task is to report the minimum number of moves from the initial position to the final position.
Input
The first line contains the number of test cases w, w <= . Then the w test cases are listed one by one. Each test case consists of lines, characters per line. The first lines are the initial board position. The remaining lines are the final board position. The i-th line of a board is the board at the i-th row. A character 'b' means a black stone, a character 'w' means a white stone, and a '*' means an empty cell.
Output
For each test case, output the minimum number of moves in one line. If it is impossible to move from the initial position to the final position, then output -.
Sample Input
w**b
*wb*
*bw*
b**w
w**b
*wb*
*bw*
bw**
w**b
*b**
**b*
bwww
w**b
*bb*
****
bwww
Sample Output
Hint
Doing simple exhaustive search without planning ahead will most likely get you into troubles.
Source
写了快200行的题目,跟前面一道题目 poj2046 很相似,用hash来保存整个图的状态,然后剩下的就是bfs的控制能力了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define ll long long
#define M 1000007
ll hash[M];
ll goal[][];
ll base[]={};
ll aimNum;
ll flag;
ll white;
ll black;
struct Node{
ll x,y;
ll mp[][];
ll step;
ll whitePx[];
ll whitePy[];
ll blackPx[];
ll blackPy[]; }st;
bool inserNum(ll ans){//hash的插入,看看是否跟之前的状态相同,其实跟vis数组标记一个意思
ll val=ans%M;
while(hash[val]!=- && hash[val]!=ans){
val=(val+)%M;
}
if(hash[val]==-){
hash[val]=ans;
return true;//可以插入返回true
}
return false;//否则返回false
} bool work(Node cnt){
ll ans=;
for(ll i=;i<;i++){
for(ll j=;j<;j++){
ans=ans+cnt.mp[i][j]*base[i*+j];//ans为整张图的hash值
}
}
if(ans==aimNum){
flag=;
}
if(inserNum(ans))
return true;
return false;
}
ll dirx[]={,,-,,-,-,,};
ll diry[]={-,,,,-,,-,};
ll bfs(){
queue<Node>q;
q.push(st);
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
if(t1.step%==){
for(ll i=;i<;i++){ ll sx=t1.whitePx[i];
ll sy=t1.whitePy[i];
//printf("%I64d %I64d\n",sx,sy);
for(ll j=;j<;j++){
t2=t1;
t2.x=sx+dirx[j];
t2.y=sy+diry[j];
if(t2.mp[t2.x][t2.y]== || t2.mp[t2.x][t2.y]==) continue;
t2.step=t1.step+;
while(t2.x>= && t2.y>= && t2.x< && t2.y< && t2.mp[t2.x][t2.y]==){
t2.x=t2.x+dirx[j];
t2.y=t2.y+diry[j];
}
t2.x=t2.x-dirx[j];
t2.y=t2.y-diry[j];
swap(t2.mp[t2.x][t2.y],t2.mp[sx][sy]);
if(work(t2)){
t2.whitePx[i]=t2.x;
t2.whitePy[i]=t2.y;
q.push(t2);
if(flag){
return t2.step;
}
}
}
}
}
else{
for(ll i=;i<;i++){
ll sx=t1.blackPx[i];
ll sy=t1.blackPy[i];
for(ll j=;j<;j++){
t2=t1;
t2.x=sx+dirx[j];
t2.y=sy+diry[j];
if(t2.mp[t2.x][t2.y]== || t2.mp[t2.x][t2.y]==) continue;
t2.step=t1.step+;
while(t2.x>= && t2.y>= && t2.x< && t2.y< && t2.mp[t2.x][t2.y]==){
t2.x=t2.x+dirx[j];
t2.y=t2.y+diry[j];
}
t2.x=t2.x-dirx[j];
t2.y=t2.y-diry[j];
swap(t2.mp[t2.x][t2.y],t2.mp[sx][sy]);
if(work(t2)){
t2.blackPx[i]=t2.x;
t2.blackPy[i]=t2.y;
q.push(t2);
if(flag){
return t2.step;
}
}
}
}
}
}
return -;
}
ll calGoal(){
ll ans=;
for(ll i=;i<;i++){
for(ll j=;j<;j++){
ans=ans+goal[i][j]*base[i*+j];
}
}
return ans;
} int main()
{
for(ll i=;i<;i++){
base[i]=base[i-]*;
}
ll t;
scanf("%I64d",&t);
while(t--){
memset(hash,-,sizeof(hash));
white=;
black=;
char s[];
for(ll i=;i<;i++){
scanf("%s",s);
for(ll j=;j<;j++){
if(s[j]=='w'){
st.x=i;
st.y=j;
st.mp[i][j]=;
st.whitePx[white]=i;
st.whitePy[white++]=j;
}
else if(s[j]=='b'){
st.x=i;
st.y=j;
st.mp[i][j]=;
st.blackPx[black]=i;
st.blackPy[black++]=j;
}
else if(s[j]=='*'){
st.mp[i][j]=;
}
}
}
for(ll i=;i<;i++){
scanf("%s",s);
for(ll j=;j<;j++){
if(s[j]=='w'){
goal[i][j]=;
}
else if(s[j]=='b'){
goal[i][j]=;
}
else if(s[j]=='*'){
goal[i][j]=;
}
}
} aimNum=calGoal();
flag=;
st.step=;
work(st);
if(flag){
printf("0\n");
}
else{
printf("%I64d\n",bfs());
} }
return ;
}