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的八数码表,要求程序复原 ...
随机推荐
- mysql插入数据时,中文乱码
MySQL 插入数据时,中文乱码问题的解决(转) 当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1 ...
- 【转】Devexpress使用之:GridControl控件(合并表头)
Devexpress系列控件功能很强大,使用起来也不太容易,我也是边摸索边使用,如果有时间我会把常用控件的使用方法整理出来的. using System; using System.Collectio ...
- HDU 2845 Beans (DP)
Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...
- vue 使用总结
1.Vuejs组件 vuejs构建组件使用 Vue.component('componentName',{ /*component*/ }): 这里注意一点,组件要先注册再使用,也就是说: Vue.c ...
- Java第三周学习日记
Day01 1.线程 进程:进程就是正在运行的应用程序.进程负责了内存空间的划分. 线程:一个进程中的代码是由线程去执行的,线程也就是其中一个执行路径. 多线程:一个进程中有多个线程可以同时执行任务. ...
- linux下单独安装oracle12.1客户端
1.安装oracle-instantclient:(默认安装即可) oracle-instantclient12.1-sqlplus-12.1.0.1.0-1.x86_64.rpmoracle-ins ...
- MyEclipse 安装activiti designer
下载activiti designer 文件地址:http://activiti.org/designer/archived/ 注意:我的是myeclipse9.0,我下载的版本是:(当我下载高版本安 ...
- IE6,IE7下滚动条没有生效解决方法
需要加个相对定位 position:relative;
- .Net调用Office Com组件的原理及问题检索com类工厂组件检索 COM 类工厂中 CLSID 为 {XXX} 的组件失败
我是在本地32位操作系统+vs2010+office2007做创建并下载Excel,ppt文件的操作没有问题,发布到64位系统的服务器上报错,最开始报错:: 1:Retrieving the COM ...
- js 计算两个日期之间的周数
//返回两个日期相差的周数 function WeeksBetw(date1, date2) { //这里的date1,date2都是Date对象 var d1 = new Date(date1); ...