双向BFS+状态压缩。

 /* 1401 */
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; struct Point_t{
char x, y;
friend bool operator ==(const Point_t &a, const Point_t &b) {
return a.x==b.x && a.y==b.y;
}
}; typedef struct Node_t {
Point_t p[];
int t;
} Node_t; const char n = ;
Node_t b1, b2;
map<int, int> tb;
char dir[][] = {
-,,,,,-,,
}; bool comp(const Point_t &a, const Point_t &b) {
if (a.x == b.x)
return a.y < b.y;
return a.x < b.x;
} int calHash(Node_t nd) {
int ret = ;
int i, j; for (i=,j=; i<; ++i,j+=) {
ret |= (nd.p[i].x << j);
ret |= (nd.p[i].y << (j+));
}
return ret;
} bool judge(Node_t &nd, int v, int d) {
int i, j, k; nd.p[v].x += dir[d][];
nd.p[v].y += dir[d][]; if (nd.p[v].x<= || nd.p[v].x>n || nd.p[v].y<= || nd.p[v].y>n)
return false; for (i=; i<; ++i) {
if (i != v) {
if (nd.p[i] == nd.p[v]) {
// jump over one piece
nd.p[v].x += dir[d][];
nd.p[v].y += dir[d][];
if (nd.p[v].x<= || nd.p[v].x>n || nd.p[v].y<= || nd.p[v].y>n)
return false;
for (j=; j<; ++j)
if (j!=v && nd.p[j]==nd.p[v])
return false;
break;
}
}
} sort(nd.p, nd.p+, comp);
return true;
} int bfs() {
int i, j, k;
queue<Node_t> Q1, Q2;
Node_t nd, tmp; sort(b1.p, b1.p+, comp);
sort(b2.p, b2.p+, comp);
Q1.push(b1);
Q2.push(b2);
tb[calHash(b1)] = ;
tb[calHash(b2)] = ; while (!Q1.empty() || !Q2.empty()) {
if (!Q1.empty()) {
nd = Q1.front();
Q1.pop();
if (nd.t < ) {
tmp = nd;
++tmp.t;
for (i=; i<; ++i) {
for (j=; j<; ++j) {
nd = tmp;
if (judge(nd, i, j)) {
k = calHash(nd);
if (tb[k] == )
return ;
if (tb[k] != ) {
Q1.push(nd);
tb[k] = ;
}
}
}
}
}
}
if (!Q2.empty()) {
nd = Q2.front();
Q2.pop();
if (nd.t < ) {
tmp = nd;
++tmp.t;
for (i=; i<; ++i) {
for (j=; j<; ++j) {
nd = tmp;
if (judge(nd, i, j)) {
k = calHash(nd);
if (tb[k] == )
return ;
if (tb[k] != ) {
Q2.push(nd);
tb[k] = ;
}
}
}
}
}
}
} return ;
} int main() {
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif b1.t = b2.t = ;
while (scanf("%d %d", &b1.p[].x, &b1.p[].y) != EOF) {
for (i=; i<; ++i)
scanf("%d %d", &b1.p[i].x, &b1.p[i].y);
for (i=; i<; ++i)
scanf("%d %d", &b2.p[i].x, &b2.p[i].y);
tb.clear();
k = bfs();
if (k)
puts("YES");
else
puts("NO");
} return ;
}

【HDOJ】1401 Solitaire的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. mysql --batch --skip-column-name --execute 使用

    mysql -h 127.0.0.1 -P 3306 -u root -p -D test --batch --skip-column-name --execute="select * fr ...

  2. iOS-本地推送(本地通知)

    第一步:注册本地通知: // 设置本地通知 + (void)registerLocalNotification:(NSInteger)alertTime { UILocalNotification * ...

  3. Android Studio学习随笔-UI线程阻塞以及优化

    我们在使用手机的时候,经常会遇到一个问题:先是卡死,然后跳出该程序无响应,是否关闭的提示(当然有可能是我们手机性能太差=.=)这是因为线程的阻塞引起的,在这里我讲述一下UI线程,一般处理程序会在UI线 ...

  4. app包中的fragment和v4包中的fragment的使用的区别

    app包中的fragment和v4包中的fragment的使用的区别 1.尽量不要用app包中的fragment,因为这个是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的 2.androi ...

  5. jquery ui 插件------------------------->sortable

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

  6. 谈谈oracle中的临时表

    --------------------创建临时表 临时保存从xml字符串解析来的数据--------------------------- 会话级别临时表SQL> create global ...

  7. Tensor神经网络进行知识库推理

    本文是我关于论文<Reasoning With Neural Tensor Networks for Knowledge Base Completion>的学习笔记. 一.算法简介 网络的 ...

  8. slf4j与log4j

    推荐使用SLF4J(Simple Logging Facade for Java)作为日志的api,SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统. ...

  9. X-Plane飞行模拟器购买安装

    要玩起X-Plane第一个步骤当然是购买了,要购买其实非常简单,只需要一张能够支持MasterCard或者其他外币结算的信用卡,在http://www.x-plane.com/官网上购买即可,比逛淘宝 ...

  10. gSoap实现ONVIF中xsd__anyType到具体结构类型的转换

    上一篇文章已经粗略计划要讨论gsoap关于序列化/解析编程. 本文则阐述一下关于gsoap生成代码的一些重要特征方法及使用.如题,下我们从ONVIF生成的C码中,挑选简单的一个类型来试验一下与xsd_ ...