[SOL] #148. 数字格子问题
说实话这题确实挺菜的。。。
废话少说,直接上代码^O^
Code:
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = , f = ; char ch = getchar();
for ( ; !isdigit(ch) ; ch = getchar()) if (ch == '-') f = -;
for ( ; isdigit(ch) ; ch = getchar()) x = x * + ch - '';
return x * f;
}
const int fac[] = { , , , , , , , , , };
const int aim = ;
const int maxCon = ;
bool vis[maxCon];
struct data {
int con[];
int hashVal;
int step;
}_begin;
queue <data> Q;
int Cantor(int s[]) {
int sum = ;
for (int i = ; i <= ; i ++) {
int cnt = ;
for (int j = i + ; j <= ; j ++) {
if (s[i] > s[j]) {
cnt ++;
}
}
sum += cnt * fac[ - i];
}
return sum + ;
}
int bfs() {
Q.push(_begin);
while (Q.size()) {
data _top = Q.front(); Q.pop(); if (vis[_top.hashVal]) {
continue;
}
if (_top.hashVal == aim) {
return _top.step;
}
vis[_top.hashVal] = true;
_top.step ++;
data _next = _top;
for (int i = ; i <= ; i ++) {
swap(_next.con[i] , _next.con[i + ]);
}
_next.hashVal = Cantor(_next.con);
Q.push(_next);
_next = _top;
int _up = _next.con[] , _dwn = _next.con[];
for (int i = ; i >= ; i --) {
_next.con[i] = _next.con[i - ];
_next.con[i + ] = _next.con[i + ];
}
_next.con[] = _up;
_next.con[] = _dwn;
_next.hashVal = Cantor(_next.con);
Q.push(_next);
_next = _top;
int two = _next.con[] , three = _next.con[] , seven = _next.con[] , six = _next.con[];
_next.con[] = six , _next.con[] = two , _next.con[] = three , _next.con[] = seven;
_next.hashVal = Cantor(_next.con);
Q.push(_next);
}
} int main() {
for (int i = ; i <= ; i ++) {
_begin.con[i] = read();
}
_begin.hashVal = Cantor(_begin.con);
printf("%d\n" , bfs());
}
[SOL] #148. 数字格子问题的更多相关文章
- Dart编程数字Number
Dart数字可以分为: int - 任意大小的整数. int 数据类型用于表示整数. double -64位(双精度)浮点数,由IEEE 754标准规定. 在 double 数据类型用于表示小数 in ...
- 利用Vue.js实现拼图游戏
之前写过一篇<基于Vue.js的表格分页组件>的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:http://www.cnblogs.com/luozhihao/p/5 ...
- iOS开发——动画篇Swift篇&动画效果的实现
Swift - 动画效果的实现 在iOS中,实现动画有两种方法.一个是统一的animateWithDuration,另一个是组合出现的beginAnimations和commitAnimation ...
- Vue.js实现拼图游戏
Vue.js实现拼图游戏 之前写过一篇<基于Vue.js的表格分页组件>的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:http://www.cnblogs.com/ ...
- Swift - 动画效果的实现方法总结(附样例)
在iOS中,实现动画有两种方法.一个是统一的animateWithDuration,另一个是组合出现的beginAnimations和commitAnimations.这三个方法都是类方法. 一,使用 ...
- Swift - 使用UIView给页面添加4×4方格
1,下面是一个利用UIView来给页面上绘制灰色方块的例子,效果图如下: 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- 8636 跳格子(dfs+记忆化搜索)
8636 跳格子 该题有题解 时间限制:2457MS 内存限制:1000K提交次数:139 通过次数:46 题型: 编程题 语言: G++;GCC Description 地上有一个n*m 的数 ...
- 20165315 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1
20165315 2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 一.安装过程 1.基本配置 创建一个新的自定义vm 选择创建自定虚拟机 操作系统选择" ...
- 2048总结 JavaScript+jQuery(取元素方便,.css,text方法)
Html部分(界面):1.开始新游戏:2.返回上一步:3.记分栏: 4.16个小格组成: 其中1,2由链接形式实现. a标签中href属性调用js方法: <a href="javasc ...
随机推荐
- Centos7 配置rsyslog客户端接收远程日志
rsyslog 因为路由器我设定每天重启,但是日志一重启就会清除,并且路由器最多只能保存1024条记录,所以我想把路由器的日志记录到一台服务器上,发现路由器包含远程日志功能 于是我就在我的centos ...
- docker可视化集中管理工具shipyard安装部署
docker可视化集中管理工具shipyard安装部署 Shipyard是在Docker Swarm实现对容器.镜像.docker集群.仓库.节点进行管理的web系统. 1.Shipyard功能 Sh ...
- Nginx优化总结
目录 Nginx性能优化概述 一. 压力测试工具实战 二.了解影响性能指标 三.系统性能优化 四.静态资源优化 Nginx性能优化概述 基于Nginx性能优化,那么在性能优化这一章,我们将分为如下几个 ...
- static作用,静态成员变量和静态成员函数
static关键字有俩作用:(1).控制存储分配:(2).控制一个名字的可见性和连接. 随着C++名字空间的引入,我们有了更好的,更灵活的方法来控制一个大项目的名字增长. 在类的内部使用s ...
- 随笔4 Dictionary<K,V>
本来说是想介绍一下Hashtable的,但是发现HashMap和Hashtable最开始的不同就是在于HashMap继承了AbstractMap,而Hashtable继承了Dictionary< ...
- bzoj3123 [Sdoi2013]森林 树上主席树+启发式合并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3123 题解 如果是静态的查询操作,那么就是直接树上主席树的板子. 但是我们现在有了一个连接两棵 ...
- 16.go语言基础学习(上)——2019年12月16日
2019年12月13日10:35:20 1.介绍 2019年10月31日15:09:03 2.基本语法 2.1 定义变量 2019年10月31日16:12:34 1.函数外必须使用var定义变量 va ...
- LeetCode--058--最后一个单词(java)
给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...
- Graph Convolutional Network
How to do Deep Learning on Graphs with Graph Convolutional Networks https://towardsdatascience.com/h ...
- handy源码阅读(三):SafeQueue类
SafeQueue类继承与信号量mutex(用于加锁),nonocopyable 定义如下: template <typename T> struct SafeQueue : privat ...