poj 2697 A Board Game(bfs+hash)
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
#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)的更多相关文章
- POJ 2697 A Board Game(Trie判重+BFS)
A Board Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 551 Accepted: 373 Descri ...
- POJ 2697 A Board Game (bfs模拟)
比较水的一道题,在4*4的棋盘上有黑白子,现在有某种移动方式,问能否通过它将棋盘从某个状态移动到另一种状态 只要想好怎么保存hash表来去重,其他就差不多了... #include <iostr ...
- 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)
http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- poj 2432 Around the world bfs+哈希
由于每个点的状态包含走过来的距离,所以要存二维的状态,但是状态总量太多,所以可以用哈希来搞. 那么就是bfs最短路,哈希记录状态了. #include <iostream> #includ ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- [BZOJ1054][HAOI2008]移动玩具 bfs+hash
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2432 Solved: 1355[Submit][Stat ...
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...
随机推荐
- java servlet+jquery+json学习小例子
引入JSON的jar包: 注意,如果包不全,页面请求servlet时,jquery ajax会返回error:function 弹出error occured!!! HTML Code: <%@ ...
- maven编写主代码与测试代码
3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目主代码位于src/main/ja ...
- android layout属性介绍
android:id 为控件指定对应的ID android:text 指定控件其中显示的文字,须要注意的是,这里尽量使用strings.xml文件其中的字符串 android:gravity 指定Vi ...
- 执行eclipse,迅速failed to create the java virtual machine。
它们必须在一排,否则会出现The Eclipse executable launcher was unable to locate its companion shared library的错误 打开 ...
- UVA11388 GCD LCM1 2 -1
题目: 给你两个数G和L,求a和b,他们的最大公约数为G和最小公倍数为L,输出a最小时的a和b.如果不存在在输出-1. Sample Input 2 1 2 3 4 Output for Samp ...
- 如何禁用不需要的HTTP方法
IIS7.0默认开启了不安全的OPTIONS和TRACE方法,建议关闭这两个方法. 以下环境为windows server 2008.IIS7.0 方法(1):web.config 在<conf ...
- 关于sql2005 与 myeclipse进行连接出现的小问题
C盘目录下没有jdbc这个文件夹,所以从网上下一个 这个是2008连接jdbc用的 正常解压第一个到相应的目录 主要是注意一个叫tcp/ip的协议,米有找到64位的 点击这里的tcp ip就哦了但是他 ...
- C# Excel 读取为Datatable
最近项目用到的读取Excel 为DataTable 兼容2003.2007.2010.记录一下,以后会用到 引用 NPOI.dll 和 EPPlus.dll using System; using S ...
- C# 窗体程序入门 之计算器
之前一直在java的B/S,这次被要求做一个C/S,其中客户端主要是界面和socket通信.因为没有使用过C#和Visual Studio的控件编程,先来个HelloWorld. 我的环境是visua ...
- 给软件增加注册功能 c#
1.软件注册类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...