[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 ...
随机推荐
- css炫酷动画收藏
1.按钮.hover.input动画(cssfx) https://cssfx.dev/ 2.svg 矢量定制icon(ikonate) https://www.ikonate.com/#conten ...
- Elasticsearch Java Low Level REST Client(嗅探器)
https://segmentfault.com/a/1190000016828977?utm_source=tag-newest#articleHeader0 嗅探器 允许从正在运行的Elastic ...
- docker安装es
下载镜像 docker pull docker.elastic.co/elasticsearch/elasticsearch:6.8.1 创建容器并映射docker run -e ES_JAVA_OP ...
- python中常用的时间操作
python中常用的时间模块有time和datetime,以下是这两个模块中常用的方法: #先引入模块 import timefrom datetime import datetiem, timezo ...
- k8s存储
·几种存储类型 1)emptyDir emptyDir Volume的生命周期与Pod一致,Pod中的所有容器都可以共享Volume,它们可以指定各自的mount路径,当Pod从节点删除时,Volum ...
- 2018-11-01-weekly
Algorithm 107. 二叉树的层次遍历 II What 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历).例如:给定二叉树 [3,9 ...
- 关于BFC的总结
虽然工作这么多年了,但是如果让我直接解释一下什么是BFC的时候,还是感觉有点不知道怎么准确的表达,下面就翻翻文档,总结一下,加深一下认识吧.大家也可以关注我的GitHub后续的更新 1.BFC的基本概 ...
- java int整数相乘溢出
int整数相乘溢出 我们计算一天中的微秒数: * * * * ;// 正确结果应为:86400000000 System.out.println(microsPerDay);// 实际上为:50065 ...
- LINUX的一些基本概念和操作
LINUX和shell的关系: linux是核,是操作系统,用于分配软硬件资源,用于支持运行环境,shell是壳,是命令解析器. linux命令: linux命令行有一个输入输出的行为,输入命令,输出 ...
- iOS项目自动打包
用的是:https://www.jianshu.com/p/a61fe38c8c29 需要上传到pgy /TestFlight 在脚本中加几句就可以了 另外一种方式,fastlane打包 首要条件: ...