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. delphi中通过CreateOleObject操控Word

    http://blog.csdn.net/csm2432/article/details/7692443

  2. AspNetPager 自定义html

    如果,上面的分页控件里面,成功和失败都是我自己添加的,使用方法如下 anp.CustomInfoHTML = "总计%RecordCount%条记录,成功" + Success + ...

  3. PHP 转义详解

    php中数据的魔法引用函数 magic_quotes_gpc  或 magic_quotes_runtime 设置为on时,为我们引用的数据碰到 单引号' 和 双引号" 以及 反斜线\ 时自 ...

  4. 算法:最大子数组own

    转载标明出处:http://i.cnblogs.com/EditPosts.aspx?postid=4726782&update=1 暴力法: // maxValue.cpp : 定义控制台应 ...

  5. RequiredFieldValidator的使用

    特別說明:1.一個Button要對頁面的多個控件進行驗證,則需要設置button和其它受控控件的ValidationGroup屬性 aspx頁面實例: <tr class="h&quo ...

  6. .NET异步编程初识async与await

    这是两个关键字,用于异步编程.我们传统的异步编程方式一般是Thread.ThreadPool.BeginXXX.EndXXX等等.把调用.回调分开来,代码的逻辑是有跳跃的,于是会导致思路不是很清晰的问 ...

  7. filter在CSS中的效果

    滤镜说明: Alpha:设置透明层次 blur:创建高速度移动效果,即模糊效果 Chroma:制作专用颜色透明 DropShadow:创建对象的固定影子 FlipH:创建水平镜像图片 FlipV:创建 ...

  8. asp web api 怎么使用put和delete。

    Method Overriding RESTful services allow the clients to act on the resources through methods such as ...

  9. dom 优酷得弹出

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 筛选DataTable数据的方法

    对DataTable进行过滤筛选的一些方法Select,dataview 当你从数据库里取出一些数据,然后要对数据进行整合,你很容易就会想到: DataTable dt = new DataTable ...