题目描述:
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的更多相关文章

  1. Hdu1401 Solitaire 2017-01-18 17:21 33人阅读 评论(0) 收藏

    Solitaire Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  2. HDU1401 BFS

    Solitaire Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  3. 1455.Solitaire(bfs状态混摇)

    Solitaire Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. Codeforces Gym 100231F Solitaire 折半搜索

    Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...

  5. ruby quiz The Solitaire Cipher

    solitaire cipher:http://en.wikipedia.org/wiki/Solitaire_(cipher) https://www.schneier.com/solitaire. ...

  6. UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))

    Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...

  7. uva 10651 - Pebble Solitaire(记忆化搜索)

    题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...

  8. Solitaire

    Solitaire Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. UVa 10651 Pebble Solitaire(DP 记忆化搜索)

    Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...

随机推荐

  1. hdu4604 Deque(最长上升子序列变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 题意:一个含有n个数栈,每次取出一个数,可以把这个数放在deque(双向队列)首部,放在尾部,或 ...

  2. python 面向对象十 __init__和__new__

    一.__init__和__new__方法 __init__ 和 __new__ 最主要的区别在于:1.__init__ 通常用于初始化一个新实例,控制这个初始化的过程,比如添加一些属性, 做一些额外的 ...

  3. bzoj 2811: [Apio2012]Guard【线段树+贪心】

    关于没有忍者的区间用线段树判就好啦 然后把剩下的区间改一改:l/r数组表示最左/最右没被删的点,然后删掉修改后的左边大于右边的:l升r降排个序,把包含完整区间的区间删掉: 然后设f/g数组表示i前/后 ...

  4. iOS 应用打包 设备兼容性问题(Build Active Architecture Only)

    在把应用打包安装到iPod Touch上面时,设备提示不兼容,所以就有几种猜想: 1.CPU架构问题,因为我手里这个iPod Touch的CPU是A5,是32位的: 2.TARGETS里面相关的设置对 ...

  5. 洛谷 P2634 [国家集训队]聪聪可可

    点分板子2333 注释都是错过的地方 #include<cstdio> #include<algorithm> using namespace std; typedef lon ...

  6. 条件DP UVA 672 Gangsters

    题目传送门 题意:n个歹徒进饭店,可变化宽度的门,范围[0, k],每个歹徒进门在ti时间进门,身材si,进去后有pi的成功值,问最大的成功值 分析:首先按照进门时间排序,dp[i][j] 表示第i个 ...

  7. iOS捷径(Workflow 2.0)拓展

    前言 iOS12 捷径(Workflow 2.0)入门 iOS12 捷径(Workflow 2.0)进阶 iOS12捷径(Workflow 2.0)实例大全 注:本文主要介绍如何获取URL Schem ...

  8. 函数的返回值return

    '''1.什么是返回值 返回值是一个函数的处理结果 2.为什么要有返回值 如果我们需要在程序中拿到函数的处理结果做进一步的处理,则需要函数必须有返回值 3.函数的返回值的应用 函数的返回值用retur ...

  9. vue全局loading组件

    本组件作用在页面加载完成前进行loader提示,提升用户体验,只需要在app.vue中引用一次,整个项目中路由切换时就可以自动进行提示(vuex版): 1. 添加vuex值和方法: import Vu ...

  10. 波哥!一个不安分的IT男

    第一篇博文,紧张,窃喜,辣眼睛! 这个订阅号主要是写给自己的,近期越来越发现记忆力不如以前了! 时光如梭,岁月荏苒,或许这两句经典的开头文比较契合自己的年纪.依稀记得几年前还在组装服务器.搬机柜.做系 ...