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\)搜索一下对于每个有用的苹果数量,满 ...
随机推荐
- memcache、memcached、groupcache的区别
对PHP语言来说,PHP使用memcache有两个模块,分别叫memcache和memcached,他们的区别看下表: 参考:http://hi.baidu.com/tony_wd/item/605e ...
- selenium python (十三)对于分页的处理
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #对于web上分页的功能,一般做如下操作: #获取总页数 # ...
- slowhttps安装及使用心得
运行及安装环境,kali. 到googlecode上下载安装包,cd到安装目录./configure 运行完毕后输入make 结束后make install 简单点就直接apt-get install ...
- C#拼音转换,将简体中文转换成拼音
1. 要进行拼音转换操作,首先要引入几个文件,也就是用于操作拼音转换的文件,就是微软提供给开发者的一个类库 Microsoft Visual Studio International Pack 1.0 ...
- 学习内容:Html5+Axure原型设计
今日主要在http://www.runoob.com/html/html5-intro.html和http://www.imooc.com/learn/9网站上学习Html的知识,head.title ...
- SpringMVC + Spring + MyBatis 学习笔记:在类和方法上都使用RequestMapping如何访问
系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 先看代码: @Requ ...
- QCon2013上海站总结 -- 前端开发
选择这个专题开始主要有两个原因:一是这次会议关于前端开发的内容不多.二是我做过几年前端开发,这个专题对我来说会容易点:) 这次QCon上海关于前端开发有一个Keynote,一个Javascript专题 ...
- The Stereo Action Dimension
Network MIDI on iOS - Part 1 This is an app I wrote to try out some ideas for networked MIDI on iP ...
- dom 封装表单控件
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- binarySearch二分查找——Javascript实现
在很早之前,我就写过了一篇也关于二分法的相关博文:JavaScript快排与原生sort的测试.当时是用二分法进行快速排序,其实和这次思路大致相当.二分查找最重要的一个条件,就是需要将数组先按照从小到 ...