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 ...
随机推荐
- bzoj 1355: [Baltic2009]Radio Transmission【kmp】
kmp复健,答案是n-next[n] #include<iostream> #include<cstdio> using namespace std; const int N= ...
- (矩阵快速幂)51NOD 1242斐波那契数列的第N项
斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, ...
- 升级Python后, yum不能用了
yum需要使用python2,而升级python3后,就会导致语法错误 更正方法: sudo vim /usr/bin/yum 然后把第一行的python改成python2就好了 之后如果出现类似的, ...
- Python爬虫库-Beautiful Soup的使用
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库,简单来说,它能将HTML的标签文件解析成树形结构,然后方便地获取到指定标签的对应属性. 如在上一篇文章通过爬虫 ...
- SAMP论文学习
SAMP:稀疏度自适应匹配追踪 实际应用中信号通常是可压缩的而不一定为稀疏的,而且稀疏信号的稀疏度我们通常也会不了解的.论文中提到过高或者过低估计了信号的稀疏度,都会对信号的重构造成影响.如果过低估计 ...
- 递推DP UVA 473 Raucous Rockers
题目传送门 题意:n首个按照给定顺序存在m张光盘里,每首歌有播放时间ti,并且只能完整的存在一张光盘里,问最多能存几首歌 分析:类似01背包和完全背包,每首歌可存可不存,存到下一张光盘的情况是当前存不 ...
- Hibernate3的hbm文件错误引用dtd文件导致项目无法启动问题处理
错误信息: org.hibernate.InvalidMappingException: Could not parse mapping document from resource /***/*** ...
- 关于cocoapods安装与使用的总结
昨天晚上研究了很入的cocoapods,在各大论坛也看过了很多方法,这里把之前的方法做一个总结. 这里我把自己遇到的一些问题,大概的说了一下.也让广告初学者少走弯路. 先是来自code4app的文章: ...
- CF940D Alena And The Heater
思路: 模拟. 实现: #include <bits/stdc++.h> using namespace std; const int INF = 1e9; ], n; string b; ...
- Linux下Eclipse连接小米手机真机调试
以前用Ubuntu 12.04可以真机调试,连上手机就可以了,上次看pear os 好看于是下了个玩玩(界面风格像mac 买不起,仿得起),这次想开发安卓发现真机调试不了了...于是乎各种找资料,各种 ...