Codeforces Gym 100231F Solitaire 折半搜索
Solitaire
题目连接:
http://codeforces.com/gym/100231/
Description
给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一:
1.走向相邻的空格
2.迈过相邻的棋子
然后给你初始状态和结束状态,问你能否得到呢?
Input
第一行给你4个初始状态棋子的坐标
第二行给你4个结束状态棋子的坐标
Output
输出能否从初始状态走到结束状态
Sample Input
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6
Sample Output
YES
Hint
题意
题解:
由于是2002年的题,所以大概怎么搜都可以(他们并没有料到10年后的电脑会跑的这么快
我用的是meet in the mid,牺牲空间来换取时间
从终点和起点都搜一遍
这样跑的很快~
代码
#include<bits/stdc++.h>
using namespace std;
set<int> T[2];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
struct node
{
vector<pair<int,int> >v;
};
int Bound(pair<int,int> x)
{
if(x.first<1||x.first>8)return 0;
if(x.second<1||x.second>8)return 0;
return 1;
}
int Hit(node tmp,pair<int,int> tt)
{
for(int i=0;i<4;i++)
if(tmp.v[i]==tt)
return 1;
return 0;
}
int Hash(node tmp)
{
int res = 0;
for(int i=0;i<4;i++)
{
res = res*10+tmp.v[i].first;
res = res*10+tmp.v[i].second;
}
return res;
}
void dfs(node st,int step,int flag)
{
sort(st.v.begin(),st.v.end());
if(step==4)
{
T[flag].insert(Hash(st));
return;
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
pair<int,int> next = st.v[i];
next.first += dx[j];
next.second += dy[j];
if(Hit(st,next))
{
next.first+=dx[j];
next.second+=dy[j];
}
if(!Bound(next))
continue;
node t=st;
t.v[i]=next;
dfs(t,step+1,flag);
}
}
}
int main()
{
node k[2];
for(int i=0;i<2;i++)
for(int j=0;j<4;j++)
{
int x,y;scanf("%d%d",&x,&y);
k[i].v.push_back(make_pair(x,y));
}
dfs(k[0],0,0);
dfs(k[1],0,1);
for(auto it:T[0])
if(T[1].find(it)!=T[1].end())
return puts("YES");
return puts("NO");
}
Codeforces Gym 100231F Solitaire 折半搜索的更多相关文章
- codeforces 880E. Maximum Subsequence(折半搜索+双指针)
E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- codeforces 1006 F(折半搜索)
F. Xor-Paths time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- CF 888E Maximum Subsequence——折半搜索
题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【LOJ#6072】苹果树(矩阵树定理,折半搜索,容斥)
[LOJ#6072]苹果树(矩阵树定理,折半搜索,容斥) 题面 LOJ 题解 emmmm,这题似乎猫讲过一次... 显然先\(meet-in-the-middle\)搜索一下对于每个有用的苹果数量,满 ...
随机推荐
- Selenium启动本地firefox的profile
ProfilesIni pi = new ProfilesIni();FirefoxProfile profile = pi.getProfile("default");WebDr ...
- 【转】GUID学习
概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是一个通过特定算 ...
- 【工作备忘】suricata
因为工作遇到的困难,我向suricata的某个作者发送了邮件. On Wed, Sep 11, 2013 at 8:22 AM, likeyi <929812468@qq.com> wro ...
- 数据结构 -- 图的最短路径 Java版
作者版权所有,转载请注明出处,多谢.http://www.cnblogs.com/Henvealf/p/5574455.html 上一篇介绍了有关图的表示和遍历实现.数据结构 -- 简单图的实现与遍历 ...
- 关闭HTML5只能提示(form上新增novalidate)
<form novalidate> <input type="text" required /> <input type="su ...
- reds pub/sub官方文档翻译
Publish / Subscribe发布/订阅 redis-py includes a PubSub object that subscribes to channels and listens f ...
- 转】腾讯云CentOS 6.6安装 Nginx
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/5290155.html 感谢! 一.下载Nginx 从Nginx的官网(http://nginx.org/en/d ...
- 为什么数据科学家们选择了Python语言?
本文由 伯乐在线 - HanSir 翻译,toolate 校稿 英文出处:Quora [伯乐在线导读]:这个问题来自 Quora,题主还补充说,“似乎很多搞数据的程序员都挺擅长 Python 的,这是 ...
- windows平台下安装python的setuptools工具
到下面的网址下载setuptools-0.6c11.win32-py2.7.exe http://pypi.python.org/pypi/setuptools#files 然后安装setuptool ...
- 每天学一点-Jquery判断checkbox是否为选中状态
if ($("#ctl00_ContentPlaceHolder1_IsLimitedService").attr("checked") ==true)