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 ;
}

poj 2697 A Board Game(bfs+hash)的更多相关文章

  1. POJ 2697 A Board Game(Trie判重+BFS)

    A Board Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 551   Accepted: 373 Descri ...

  2. POJ 2697 A Board Game (bfs模拟)

    比较水的一道题,在4*4的棋盘上有黑白子,现在有某种移动方式,问能否通过它将棋盘从某个状态移动到另一种状态 只要想好怎么保存hash表来去重,其他就差不多了... #include <iostr ...

  3. 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...

  4. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  5. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  6. poj 2432 Around the world bfs+哈希

    由于每个点的状态包含走过来的距离,所以要存二维的状态,但是状态总量太多,所以可以用哈希来搞. 那么就是bfs最短路,哈希记录状态了. #include <iostream> #includ ...

  7. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  8. [BZOJ1054][HAOI2008]移动玩具 bfs+hash

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2432  Solved: 1355[Submit][Stat ...

  9. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

随机推荐

  1. Spring 的优秀工具类盘点第 1 部分

    文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...

  2. 面试时遇到的SQL

    CustomerID DateTime ProductName Price C001 2014-11-20 16:02:59 123 PVC 100 C001 2014-11-19 16:02:59 ...

  3. POJ 3450 Corporate Identity (KMP+暴搞)

    题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...

  4. servlet下载,解决文件名中有中文下载路径出现乱码不能正常下载问题

    方法很多种,我只试用了两种. 主页面JSP中引入下载功能所需的js文件.引入的时候设置编码格式例如 <script type="text/javascript" charse ...

  5. F5(调试)和服务器控件

    一.调试 背景: 今天调试的时候发现我进入的网址是http://×××.com:7813/webaspx/System/Login.aspx(由于代码在公司,我就没有截图,等了半天显示无法加载该页面) ...

  6. C#编写Windows服务程序图文教程(转载)

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  7. easyui combobox赋值

    $('#cc').combobox('setValue','bitem3').combobox('setText','bitem3')

  8. EF数据存贮问题二之“无法定义这两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象”

    “无法定义这两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象”,这是在EF中,一对多关系表,有外键的类保存至数据库中出现的错误. 我原来是用JAVA开发的,习惯性的处理一对多 ...

  9. Homebrew -- Mac软件管家(套件管理yun……)

    也许是之前使用linux系统的时候总是习惯使用wget 在mac中只有curl,有点略显不习惯 于是乎某天在搜索mac开发者的时候发现了Homebrew这个东西 ok,是那么句话--惰性是人的天性 有 ...

  10. Http Clinet使用

    Http Client是个apache下的一个开源包,用于使用http协议访问服务的java代码编写. Http Client的主要功能: (1)实现了所有 HTTP 的方法(GET,POST,PUT ...