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 折半搜索的更多相关文章

  1. codeforces 880E. Maximum Subsequence(折半搜索+双指针)

    E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  3. 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  ...

  4. codeforces 1006 F(折半搜索)

    F. Xor-Paths time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  5. CF 888E Maximum Subsequence——折半搜索

    题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...

  6. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  7. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  8. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  9. 【LOJ#6072】苹果树(矩阵树定理,折半搜索,容斥)

    [LOJ#6072]苹果树(矩阵树定理,折半搜索,容斥) 题面 LOJ 题解 emmmm,这题似乎猫讲过一次... 显然先\(meet-in-the-middle\)搜索一下对于每个有用的苹果数量,满 ...

随机推荐

  1. [搜片神器]之DHT网络爬虫的C++程序初步开源

    回应大家的要求,特地整理了一开始自己整合的代码,这样最简单,最直接的可以分析流程,至于文章里面提供的程序界面更多,需要大家自己开发. 谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: ht ...

  2. Loadrunner脚本之C语言文件处理函数

    一.打开文件 打开文件使用库函数中的fopen函数.fopen函数会为要打开的文件新建一个流,然后返回一个指向file型对象的指针,该file型对象中保存了控制这个流所需要的信息. fp=fopen( ...

  3. selenium python (四)键盘事件

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #在实际测试过程中,有时候我们需要使用tab键将焦点转移到下一个需要操作 ...

  4. 数往知来C#之接口 值类型与引用类型 静态非静态 异常处理 GC垃圾回收 值类型引用类型内存分配<四>

    C# 基础接口篇 一.多态复习 使用个new来实现,使用virtual与override    -->new隐藏父类方法 根据当前类型,电泳对应的方法(成员)    -->override ...

  5. 设计模式 工厂-Factory

    在开始笔记之前先推荐一个网站:http://design-patterns.readthedocs.org/zh_CN/latest/index.html 网站对每一个Pattern都有详尽的解说.并 ...

  6. effective c++:引用传递与值传递,成员函数与非成员函数

    以pass-by-reference-to-const 替换pass-by-value 考虑以下class继承体系 class Person { public: Person(); // parame ...

  7. WinForm 中两个窗口之间传递数据

    方法有很多种,这里介绍项目中使用的两种 一.通过委托+事件的方法进行传值 (点击Form2中的button1按钮,将会把Form2中的textbox.text 传给Form1中的 lable.text ...

  8. WS之cxf与spring整合1

    1.在web.xml中加入CXFServlet: <!-- 下面表示所有来自/cxfservice/*的请求,都交给 CXFServlet来处理 .--> <servlet>  ...

  9. POJ 1039 Pipe(直线和线段相交判断,求交点)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8280   Accepted: 2483 Description ...

  10. LightOJ 1370 - Bi-shoe and Phi-shoe (欧拉函数思想)

    http://lightoj.com/volume_showproblem.php?problem=1370 Bi-shoe and Phi-shoe Time Limit:2000MS     Me ...