ZOJ - 1505 Solitaire 【双向BFS】
题目链接
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1505
题意
一个8 * 8 的棋盘上面有四个棋子
棋子可以上下左右移动,如果隔壁有个棋子 那就可以跳一步,只能跳一步。
给出 初始状态,和末尾状态 求能不能在8步之内达到
思路
如果是单向BFS (4 * 4)^ 8 = 2 ^ 32 个状态数
太大了
采用双向广搜
状态数 就是
(4 * 4)^ 4 * 2 = 2 ^ 17
因为内存只有32mb
然后我想 用八进制的数字 来存状态
然后转化成十进制
就是 8 ** 8 = 16777216
16777216 * 4 / 1024 = 65536kb = 64mb
爆内存了。。
然后后来想想,既然用了双向广搜来减少时间复杂度了,,不如直接用map 标记 也只是增加了log 的时间复杂度
就试试了。。
对了 那个状态 对四个棋子的位置 一定要排序后再转值 不然可能两个状态是一样的 但是得到的值确实不一样的
然后就A了。
AC代码
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a));
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define L(on) ((on)<<1)
#define R(on) (L(on) | 1)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define syn_close ios::sync_with_stdio(false); cin.tie(0);
#define sp system("pause");
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double EI = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int maxn = (int)4e3 + 10;
const int MAXN = (int)1e2 + 10;
const int MOD = (int)1e9 + 7;
int readint()
{
int num; scanf("%d", &num);
return num - 1;
}
struct node
{
pii G[4];
int step, value, vis;
bool operator == (const node& r)const
{
for (int i = 0; i < 8; i++)
if (r.G[i] != G[i])
return false;
return true;
}
void tran()
{
value = 0;
sort(G, G + 4, [](pii a, pii b) { if (a.fi == b.fi) return a.se < b.se; else return a.fi < b.fi; });
for (int i = 0; i < 4; i++) value = value * 10 + G[i].fi, value = value * 10 + G[i].se;
}
void read(int l, int r)
{
for (int i = l; i < r; i++) { G[i].fi = readint(); G[i].se = readint();}
step = 0; tran();
}
}st, tar;
void init()
{
st.G[0].se = readint(); st.G[0].fi--;
st.read(1, 4); tar.read(0, 4);
st.vis = 0; tar.vis = 1;
}
int Move[2][4][2] =
{
-1, 0,
1, 0,
0,-1,
0, 1,
-2, 0,
2, 0,
0,-2,
0, 2,
};
bool ok(int x, int y)
{
if (x < 0 || x >= 8 || y < 0 || y >= 8)
return false;
return true;
}
void bfs()
{
if (st == tar)
{
puts("YES");
return;
}
queue <node> q[2]; q[0].push(st); q[1].push(tar);
map <int, pii> mp[2]; mp[0][st.value] = pii(1, 0); mp[1][tar.value] = pii(1, 0);
while (!q[0].empty() && !q[1].empty())
{
node u;
if (q[0].size() < q[1].size()) { u = q[0].front(); q[0].pop(); }
else { u = q[1].front(); q[1].pop(); }
if (u.step >= 8) continue;
map <pii, int> visit;
for (int i = 0; i < 4; i++)
visit[u.G[i]] = 1;
for (int i = 0; i < 4; i++)
{
int x = u.G[i].fi, y = u.G[i].se;
for (int j = 0; j < 4; j++)
{
int nx = x + Move[0][j][0];
int ny = y + Move[0][j][1];
if (ok(nx, ny))
{
if (visit[pii(nx, ny)])
{
nx = x + Move[1][j][0];
ny = y + Move[1][j][1];
if (ok(nx, ny) && visit[pii(nx, ny)] == 0)
{
node v = u;
swap(v.G[i].fi, nx); swap(v.G[i].se, ny);
v.step++; v.tran();
if (mp[v.vis][v.value].fi == 0)
{
mp[v.vis][v.value] = pii(1, v.step);
pii tmp = mp[v.vis ^ 1][v.value];
if (tmp.fi == 1 && tmp.se + v.step <= 8)
{
puts("YES");
return;
}
q[v.vis].push(v);
}
}
}
else
{
node v = u;
swap(v.G[i].fi, nx); swap(v.G[i].se, ny);
v.step++; v.tran();
if (mp[v.vis][v.value].fi == 0)
{
mp[v.vis][v.value] = pii(1, v.step);
pii tmp = mp[v.vis ^ 1][v.value];
if (tmp.fi == 1 && tmp.se + v.step <= 8)
{
puts("YES");
return;
}
q[v.vis].push(v);
}
}
}
}
}
}
puts("NO");
return;
}
int main()
{
while (scanf("%d", &st.G[0].fi) != EOF)
{
init(); bfs();
}
}
ZOJ - 1505 Solitaire 【双向BFS】的更多相关文章
- Hdu1401-Solitaire(双向bfs)
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...
- HDU 1401 Solitaire 双向DFS
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
随机推荐
- Android实现炫酷SVG动画效果
svg是眼下十分流行的图像文件格式了,svg严格来说应该是一种开放标准的矢量图形语言,使用svg格式我们能够直接用代码来描画图像,能够用不论什么文字处理工具打开svg图像.通过改变部分代码来使图像具有 ...
- 结合jquery的前后端加密解密 适用于WebApi的SQL注入过滤器 Web.config中customErrors异常信息配置 ife2018 零基础学院 day 4 ife2018 零基础学院 day 3 ife 零基础学院 day 2 ife 零基础学院 day 1 - 我为什么想学前端
在一个正常的项目中,登录注册的密码是密文传输到后台服务端的,也就是说,首先前端js对密码做处理,随后再传递到服务端,服务端解密再加密传出到数据库里面.Dotnet已经提供了RSA算法的加解密类库,我们 ...
- SPI协议介绍
一.概述 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在外围设备微控制 ...
- Linux Apache安装加载mod_deflate模块
为了开启apache服务器中的gzip压缩功能,mod_deflate模块是必须安装加载的.现在介绍如何安装.1.进入到mod_deflate.c目录 cd /lamp/httpd-2.2.20/mo ...
- Nginx 配置指令的执行顺序
在一个 location 中使用 content 阶段指令时,通常情况下就是对应的 Nginx 模块注册该 location 中的“内容处理程序”.那么当一个 location 中未使用任何 cont ...
- Arbitrage - poj 2240 (Bellman-ford)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17374 Accepted: 7312 Description Ar ...
- 【SoapUI、Postman、WebServiceStudio、Jmeter】接口测试工具结合测试webservice接口(发送XML格式参数)
目录: SoapUI测试webservice接口,发送XML格式参数 Postman测试webservice接口,发送XML格式参数 WebServiceStudio.exe测试webservice接 ...
- android事件拦截处理机制具体解释
前段时间刚接触过android手机开发.对它的事件传播机制不是非常了解,尽管网上也查了相关的资料,可是总认为理解模模糊糊,似是而非,于是自己就写个小demo測试了一下. 总算搞明确了它的详细机制.写下 ...
- Windows下搭建React Native Android开发环境
准备工作 安装JDK 安装Android SDK 安装C++环境 安装node.js 安装react-native命令行工具 创建项目 运行packager 运行模拟器 安卓运行 安卓调试 安装JDK ...
- Coursera machine learning 第二周 quiz 答案 Linear Regression with Multiple Variables
https://www.coursera.org/learn/machine-learning/exam/7pytE/linear-regression-with-multiple-variables ...