Hdu1401-Solitaire(双向bfs)
There are four identical pieces on the board. In one move it is allowed to:
> move a piece to an empty neighboring field (up, down, left or right),
> jump over one neighboring piece to an empty field (up, down, left or right).
There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.Write a program that:
> reads two chessboard configurations from the standard input,
> verifies whether the second one is reachable from the first one in at most 8 moves,
> writes the result to the standard output.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int mod=;
const int maxn=;
int dx[]={-,,,},dy[]={,-,,};
bool in(int x,int y){ return x>=&&x<=&&y>=&&y<=; }
struct point //球的坐标
{
int x,y;
point(int x=,int y=):x(x),y(y){}
bool operator < (const point& t) const
{
if(x!=t.x) return x<t.x;
return y<t.y;
}
};
struct node
{
point p[]; //每个节点保存4个球的坐标,并且是排好序的
}nod[][maxn]; //开二维用于双向搜
struct Hash
{
int v,k,nid,next; //保存哈希值,0或1,下标,next指针
}ha[mod+maxn];
int f[],r[],hash_id; //队首队尾指针
bool Same(int k1,int a,int k2,int b) //判断两个结构体是否完全相同
{
for(int i=;i<;i++)
{
if(nod[k1][a].p[i].x!=nod[k2][b].p[i].x) return false;
if(nod[k1][a].p[i].y!=nod[k2][b].p[i].y) return false;
}
return true;
}
int GetHash(point p[]) //得到哈希值
{
int ret=,k=;
for(int i=;i<;i++)
{
ret+=p[i].x*k; k*=; //k是系数
ret+=p[i].y*k; k*=;
}
return ret;
}
int Insert_Hash(int v,int k,int nid) //插入
{
int a=v%mod;
int p=ha[a].next;
while(p!=-)
{
if(ha[p].v==v&&Same(ha[p].k,ha[p].nid,k,nid)) return ha[p].k==k?:; //有相同的状态,k值相同返回0,不同返回1
p=ha[p].next;
}
p=++hash_id;
ha[p].v=v; ha[p].k=k; ha[p].nid=nid; //增加新的节点
ha[p].next=ha[a].next; ha[a].next=p;
return -;
}
bool Has(node& t,int x,int y)
{
for(int i=;i<;i++) if(t.p[i].x==x&&t.p[i].y==y) return true; //此处是球
return false;
}
bool AddNode(node& t,int i,int j,int k)
{
int x=t.p[i].x;
int y=t.p[i].y;
int nx=x+dx[j];
int ny=y+dy[j];
if(!in(nx,ny)) return false; //出界
if(Has(t,nx,ny)) nx+=dx[j],ny+=dy[j]; //有球
if(!in(nx,ny)) return false; //出界
if(Has(t,nx,ny)) return false; //还有球
node& tt=nod[k][r[k]];
tt=t;
tt.p[i].x=nx; tt.p[i].y=ny;
sort(tt.p,tt.p+); //排序
int a=Insert_Hash(GetHash(tt.p),k,r[k]);
if(a==) return true; //找到解
else if(a==-) r[k]++; //增加新节点
return false;
}
bool bfs(int k)
{
int en=r[k];
while(f[k]<en)
{
node& t=nod[k][f[k]++];
for(int i=;i<;i++) //4个球4个方向
for(int j=;j<;j++) if(AddNode(t,i,j,k)) return true;
}
return false;
}
bool solve()
{
if(Same(,,,)) return true; //相同
int step=;
f[]=f[]=; r[]=r[]=;
for(int i=;i<mod;i++) ha[i].next=-;
hash_id=mod-;
Insert_Hash(GetHash(nod[][].p),,);
Insert_Hash(GetHash(nod[][].p),,);
while(f[]<r[]||f[]<r[])
{
if(step>=) return false;
step++;
if(bfs()) return true;
if(bfs()) return true;
}
return false;
}
int main()
{
int x,y;
while(scanf("%d%d",&x,&y)!=EOF)
{
nod[][].p[]=point(x,y);
for(int i=;i<;i++)
{
scanf("%d%d",&x,&y);
nod[][].p[i]=point(x,y);
}
for(int i=;i<;i++)
{
scanf("%d%d",&x,&y);
nod[][].p[i]=point(x,y);
}
sort(nod[][].p,nod[][].p+);
sort(nod[][].p,nod[][].p+);
if(solve()) printf("YES\n");
else printf("NO\n");
}
return ;
}
Hdu1401-Solitaire(双向bfs)的更多相关文章
- HDU1401(双向BFS)
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1401 给你8*8的棋盘和4个棋子初始位置.最终位置,问你能否在8次操作后达到该状态. 思路: 双向BFS, ...
- HDU 1401 Solitaire 双向DFS
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
随机推荐
- HDU3564 --- Another LIS (线段树维护最值问题)
Another LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M【图片 价格 品牌 报价】-京东
法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M[图片 价格 品牌 报价]-京东 法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M
- C++程序面试笔迹六
1.const和static的作用 用最熟悉的语言: 2.阶乘求和 如:1!+2!+3!+4!+……+n!=sum? 3.删除掉字符串中相同的字母只留有一个 如: akkkjjtyy——> ...
- Java中线程池的学习
线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理.当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程 ...
- THREE.JS + Blender(obj、mtl加载代码)
2016-11-04 09:23:17 THREE.REVISION "81dev" Blender "2.78" 1.加载OBJ.MTL文件 // T ...
- python高级编程:有用的设计模式2
# -*- coding: utf-8 -*- __author__ = 'Administrator' #python高级编程:有用的设计模式 #代理 """ 代理对一 ...
- Css实现垂直水平居中的六种方法
经常在项目中用到,今天总结了一下: 演示地址:http://codepen.io/anon/pen/xGdpOa 以下两个类为公共类,便于更好的显示效果,非核心代码 .common{ width: 6 ...
- ZigBee心电传输(二)
不管怎样,还是在高手的帮助下完成了前面的硬件部分,现在进行ZigBee的心电AD采集和转换.需要把ZigBee重新拾起来. 首先明确下目标和思路吧,目标是将模拟心电信号通过AD转换,变为数字信号,再用 ...
- [Regex Expression] Find Sets of Characters
Regular Expression Character Classes define a group of characters we can use in conjunction with qua ...
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(下) .
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/25466665 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...