HDU1401 Solitaire
题目描述:
8×8的棋盘上有4个棋子,棋子的运动方法如下:
1.如果其上/下/左/右一格没有棋子,则可以去;2.如果其上/下/左/右一格有棋子,而且沿原方向再跳一步没有,则可以去。
给出初始结束位置,问8步以内能否走到?
题解:
双向BFS。
从初始结束位置一起跑4步。
也称meet_in_the_middle
代码:
#include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
bool ck(int x,int y){return x>=&&x<=&&y>=&&y<=;}
struct P
{
int x,y;
P(){}
P(int x,int y):x(x),y(y){}
};
bool cmp(P a,P b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
struct sp
{
P s[];
int v()
{
int hs = ;
for(int i=;i<=;i++)hs=hs*+(s[i].x-)*+s[i].y-;
return hs;
}
bool emp(int x,int y)
{
if(!ck(x,y))return ;
for(int i=;i<=;i++)if(s[i].x==x&&s[i].y==y)return ;
return ;
}
void bal()
{
sort(s+,s+,cmp);
}
}a,b,u,t;
int dx[]={-,,,},dy[]={,,-,};
int bfs()
{
map<int,int>mp1,mp2;
queue<sp>q1,q2,tmp;
int hs1,hs2,x,y,tx,ty;
hs1 = a.v(),hs2=b.v();
if(hs1==hs2)return ;
mp1[hs1]=mp2[hs2]=;
q1.push(a),q2.push(b);
for(register int dep=;dep<=;dep++)
{
while(!q1.empty())
{
u = q1.front();
q1.pop();
for(register int i=;i<=;i++)
{
x = u.s[i].x,y = u.s[i].y;
for(register int j=;j<;j++)
{
t=u;
tx=x+dx[j],ty=y+dy[j];
if(!t.emp(tx,ty))
{
tx+=dx[j],ty+=dy[j];
}
if(t.emp(tx,ty))
{
t.s[i].x=tx,t.s[i].y=ty;
t.bal();
hs1 = t.v();
if(mp2[hs1])return dep+mp2[hs1];
if(!mp1[hs1])
{
mp1[hs1]=dep;
tmp.push(t);
}
}
}
}
}
while(!tmp.empty())q1.push(tmp.front()),tmp.pop();
while(!q2.empty())
{
u = q2.front();
q2.pop();
for(register int i=;i<=;i++)
{
x = u.s[i].x,y = u.s[i].y;
for(register int j=;j<;j++)
{
t=u;
tx=x+dx[j],ty=y+dy[j];
if(!t.emp(tx,ty))
{
tx+=dx[j],ty+=dy[j];
}
if(t.emp(tx,ty))
{
t.s[i].x=tx,t.s[i].y=ty;
t.bal();
hs2 = t.v();
if(mp1[hs2])return dep+mp1[hs2];
if(!mp2[hs2])
{
mp2[hs2]=dep;
tmp.push(t);
}
}
}
}
}
while(!tmp.empty())q2.push(tmp.front()),tmp.pop();
}
return ;
}
int main()
{
while(scanf("%d%d",&a.s[].x,&a.s[].y)>)
{
for(int i=;i<=;i++)scanf("%d%d",&a.s[i].x,&a.s[i].y);
for(int i=;i<=;i++)scanf("%d%d",&b.s[i].x,&b.s[i].y);
sort(a.s+,a.s+,cmp),sort(b.s+,b.s+,cmp);
printf(bfs()?"YES\n":"NO\n");
}
return ;
}
HDU1401 Solitaire的更多相关文章
- Hdu1401 Solitaire 2017-01-18 17:21 33人阅读 评论(0) 收藏
Solitaire Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- HDU1401 BFS
Solitaire Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 1455.Solitaire(bfs状态混摇)
Solitaire Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Codeforces Gym 100231F Solitaire 折半搜索
Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...
- ruby quiz The Solitaire Cipher
solitaire cipher:http://en.wikipedia.org/wiki/Solitaire_(cipher) https://www.schneier.com/solitaire. ...
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- uva 10651 - Pebble Solitaire(记忆化搜索)
题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...
- Solitaire
Solitaire Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- UVa 10651 Pebble Solitaire(DP 记忆化搜索)
Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...
随机推荐
- Swift4 模式, 枚举型
创建: 2018/03/05 完成: 2018/03/05 更新: 2018/03/10 改变标题 [Swift4 模式] -> [Swift4 模式, 枚举型] 补充RawRepresenta ...
- bzoj 3307: 雨天的尾巴【树剖lca+树上差分+线段树合并】
这居然是我第一次写线段树合并--所以我居然在合并的时候加点结果WAWAWAMLEMLEMLE--!ro的时候居然直接指到la就行-- 树上差分,每个点建一棵动态开点线段树,然后统计答案的时候合并即可 ...
- 文件系统访问控制ACL设置
1.传统Linux文件系统权限的问题 传统Linux文件系统有三类用户:文件属主-u,组用户-g,其它用户-o,以及三种访问权限:读-r,写-w,执行或目录进入-x,但很多时候并不能满足对文件访问的细 ...
- spring boot 项目发布运行
1. maven install 发布jar包 2. java -jar webservice.jar 启动jar包
- [转]广义正交匹配追踪(gOMP)
广义正交匹配追踪(Generalized OMP, gOMP)算法可以看作为OMP算法的一种推广,由文献[1]提出,第1作者本硕为哈工大毕业,发表此论文时在Korea University攻读博士学位 ...
- 牛客小白月赛5-J-时间(time) (简单模拟)
题目描述 Apojacsleam是一个喜欢特殊时刻的人. 他定义了一个时刻,若电子表显示ab:ba(24小时制),则该时刻为“回文时刻”(可以有前导零).例如00:00就是回文时刻. 给定一个时刻,求 ...
- Service官方教程(5)后台服务发送通知、把服务变前台服务。
1.Sending Notifications to the User (发送通知) Once running, a service can notify the user of events usi ...
- 关于minSdkVersion="8" 升级appcompat_v7包主题"Theme.AppCompat.Light"等不存在的问题
关于minSdkVersion="8" 升级后,又不想用 appcompat_v7包, 那么appcompat_v7主题"Theme.AppCompat.Light&qu ...
- 外文翻译 《How we decide》 Introduction
书籍PDF版地址:How we decide 本文为书籍导言部分的拙劣翻译. 当我驾驶着波音737驶向东京成田国际机场时,飞机的引擎突然起火了.此时我们正处于7000英尺的高空,机场的跑道就在不远的前 ...
- 了解java内存回收机制-博客导读
此文作为读优质博客前的导读文 1.如何判断对象是否该回收 该对象是否被引用,是否处于不可达状态 2.对象的引用机制 强引用.软引用.弱引用.虚引用 3.垃圾回收机制如何回收.算法. 串行回收.并行回收 ...