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的八数码表,要求程序复原 ...
随机推荐
- RequireJS入门(一)
RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...
- 使用Qt Style Sheets制作UI特效
引言 作为一套GUI框架,Qt是非常强大的.(注:Qt 不仅是一套优秀的GUI框架,同时也是一套出色的应用程序框架).在UI的制作方面Qt为广大开发者提供了一套强大而易用的工具,她就是——Qt Sty ...
- Apache与Tomcat区别联系
监控服务(师傅让我监控Tomcat,我知道Apache,所以以为他两是一个东东.结果半天就没有找到Tomcat的服务进程,还理直气壮的说:找不到Apache......希望这篇简单的,白话分析,能让还 ...
- HTML与CSS入门——第十三章 使用框架
知识点: 1.建立框架集的方法 2.在框架和窗口之间链接的方法 3.使用内联框架的方法 13.1 什么是框架: 框架是浏览器窗口中的一个矩形区域,每个框架显示的是一个完整的页面. 作者不建议使用框架, ...
- 先装Net Framework 后 装 IIS的处理办法
先装IIS话,后面装Net Framework时候会自动注册 处理aspx和ashx等的处理扩展程序 先装Net Framework 后 装 IIS.扩展程序注册在命令:aspnet_regiis - ...
- Windows服务简单实例
1.定时器使用 partial class TimerService : ServiceBase { public TimerService() { InitializeComponent(); } ...
- ios9API基础知识总结(二)
UIAlertView(警告框) UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"标题" message:@&qu ...
- SQL 经典练习
SQL 基础练习 CREATE TABLE STUDENT(SNO VARCHAR(3) NOT NULL, SNAME VARCHAR(4) NOT NULL,SSEX VARCHAR(2) NOT ...
- QML添加右键菜单
MouseArea { id: mouseRegion anchors.fill: parent; acceptedButtons: Qt.LeftButton | Qt.RightButton // ...
- Qt中gb2312/GBK的URL编解码函数
编码函数: QByteArray encodeURI(QString str) { QByteArray array; QTextCodec *codec=QTextCodec::codecForNa ...