双向广搜-HDU1401 Solitaire
文章目录
- 双向广搜
- 例题
- 题意
- 分析
- 代码
- 小结
双向广搜
什么是双向广搜? 如果把bfs想象成在平静的池塘丢一颗石头,激起的波浪一层层扩散到整个空间直到到达目标,就得到起点到终点的最优路径。那么双向广搜就是在起点和终点同时丢石头,两个波浪将在中间某个位置相遇,即得到最优路径。
例题
传送门: HDU-1401
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively. There are four identical pieces on the board. In one move it is allowed to: move a piece to an empty neighboring field (up, down, left or right), jump over one neighboring piece to an empty field (up, down, left or right).
>There are 4 moves allowed for each piece in the configuration shown above. As an example let’s consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right. Write a program that: reads two chessboard configurations from the standard input, verifies whether the second one is reachable from the first one in at most 8 moves, writes the result to the standard output.
input:
Each of two input lines contains 8 integers a1, a2, …, a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece - the row number and the column number respectively. Process to the end of file.
output:
The output should contain one word for each test case - YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
Sample Input:
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6
Sample Output:
YES
题意
在8*8棋盘有四颗棋子,输入开始状态和目标状态的棋子坐标,每次可以移动一个棋子也可以跳过另一个棋子,问能否在8步内走到目标状态。
分析
用双向广搜求解,8个坐标值位压缩成为10进制作为hash值(或者8维数组?)并用unordered_set判重,当hash值出现在另一个分支即相遇。注意剪枝:每个棋子最多只能走4步,因为如果多于4步,那么他们步数之和就会大于8。
代码
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
typedef long long ll;
int stepx[4] = { -1,1,0,0 };
int stepy[4] = { 0,0,-1,1 };
struct node {
int p[4], vis;
bool check(int v) { //查重
for (int i : p)
if (i == v)
return 1;
return 0;
}
int hash() {
int a[4];
memcpy(a, p, sizeof(a));
sort(a, a + 4);
int res = 0;
for (int i : a)
res = res * 100 + i;
return res;
}
};
unordered_set<int> st[2]; //存hashc
queue<node> q[2];
void clear() {
for (int i = 0; i < 2; i++) {
queue<node>c;
swap(q[i], c);
st[i].clear();
}
}
bool ans(int i, int hash) {//队列下标和待查hash
if (st[i].find(hash) != st[i].end()) //相遇
return true;
return false;
}
bool dbfs() {
while (!q[0].empty() || !q[1].empty()) {
for (int i = 0; i < 2; ++i) { //2个队列
if (!q[i].empty()) {
node cur = q[i].front(); q[i].pop();
cur.vis++;
for (int j = 0; j < 4; ++j) { //4个点
int x = cur.p[j] / 10, y = cur.p[j] % 10;
for (int k = 0; k < 4; ++k) { //4个方向
int nx = x + stepx[k], ny = y + stepy[k];
if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8 && cur.check(nx * 10 + ny))
nx += stepx[k], ny += stepy[k];
if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8 && cur.vis <= 4 && //小于四步
!cur.check(nx * 10 + ny)) {
cur.p[j] = nx * 10 + ny;
if (st[i].find(cur.hash()) == st[i].end()) {
if (ans(!i, cur.hash()))return true;
q[i].push(cur), st[i].insert(cur.hash()); //入队
}
}
}
cur.p[j] = x * 10 + y; //回溯
}
}
}
}
return false;
}
int main() {
while (true) {
clear();
node t;
int a;
t.vis = 0;
for (int k = 0; k < 2; ++k) {
for (int i = 0; i < 4; ++i) {
t.p[i] = 0;
for (int j = 0; j < 2; ++j) {
if (scanf("%d", &a) == EOF)exit(0);
t.p[i] = t.p[i] * 10 + a;
}
}
q[k].push(t);
st[k].insert(t.hash());
}
if (ans(0, q[1].front().hash()) || dbfs())
printf("YESn");
else printf("NOn");
}
return 0;
}
小结
1.如果爆栈:考虑在入队前做答案检查,出队才检查会多一轮消耗。 2.如何保存中间状态?位压缩.位运算.unordered_set…数组? 3.String判重TLE让你怀疑人生,string复制会超时,还是老老实实哈希或者康托展开什么的。 4.神奇剪枝起死回生,一定要注意考虑剪枝,因题制宜。
原创不易,请勿转载(
本不富裕的访问量雪上加霜) 博主首页:https://blog.csdn.net/qq_45034708
- Pandas——高效的数据处理Python库
- Oracle中的段(r10笔记第81天)
- 转-- Golang中timer定时器实现原理
- Golang语言 -并行程序
- 深度学习中的优化问题以及常用优化算法
- GoldenGate简单复制环境的搭建(r10笔记第79天)
- 在Golang语言中统计程序执行时间
- 经典面试问题: Top K 之 ---- 海量数据找出现次数最多或,不重复的。
- 每天一个Linux命令(2):cd命令
- Golang语言为类型添加方法
- 浅谈 Glide - BitmapPool 的存储时机 & 解答 ViewTarget 在同一View显示不同的图片时,总用同一个 Bitmap 引用的原因
- 100个Numpy练习【2】
- 浅谈 maxMemory , totalMemory , freeMemory 和 OOM 与 native Heap
- 100个Numpy练习【1】
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 为啥Flutter Hooks没有受到太多关注和青睐?
- 二叉搜索树删除节点 动画演示
- 并发与竞态 (自旋锁)
- [Maven][taglist-maven-plugin]告警[WARNING] Using legacy tag format
- [Maven][l10n-maven-plugin]告警[WARNING] No dictionary file under folder
- Python基础之多文件项目的演练
- Python中的命名空间和作用域(1)
- 浅谈Mybatis持久化框架在Spring、SSM、SpringBoot整合的演进及简化过程
- 玩转注册表,这几个windowsAPI函数就够了
- 施工专题第11篇:Python 包和模块使用总结
- Node.js-具有示例API的基于角色的授权教程
- 删库时,我后悔没早学会的数据库知识
- SwiftUI:使自定义类型符合 Comparable 协议
- 【CCF】线性分类器
- 计数二进制子串