[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 ...
随机推荐
- 解决代码加载慢,以至于乱码—————VUE
vue.js解决开始代码加载,以至于乱码 vue.js通过几行代码可以解决这个问题 css: [v-cloak] { display: none; } html: <div id="a ...
- Solr的学习使用之(八)facet实战
以下为统计media的数量功能,通过solrj,采用了facet方法,类似于sql的分组group by查询:这边的代码只是获取总媒体数量,其实他还有其他功能,去循环solrList的话,可以获取各个 ...
- vue Base64图片压缩上传OSS
this.compress(result, 800, 0.5).then(val => { //得到压缩图片 let data = val; that.file = that.dataURLto ...
- 解决nginx proxy_pass反向代理cookie,session丢失的问题
参考地址:https://www.jianshu.com/p/34abe7eb6f0b 为什么cookie 会丢失? 比如说一个没有经过代理的地址 : http://127.0.0.1/project ...
- bzoj4128 Matrix 矩阵 BSGS
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4128 题解 想了十分钟没有任何思路. 然后一眼瞥见一句话"数据保证在 \(p\) 内 ...
- rocketmq启动broker内存占用过大的问题
解决方法: 修改broker启动脚本runbroker.sh里面的jvm参数 JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g" ...
- js中关键字 const , let , var 的用法区别
1.const定义的变量不可以修改,而且必须初始化. 2.var定义的变量可以修改,如果不初始化会输出undefined,不会报错. 3.let是块级作用域,函数内部使用let定义后,对函数外部无影响 ...
- python 在不同CPU上同时运行多个程序
出处/From https://www.quora.com/If-you-run-Python-under-a-dual-core-CPU-then-can-you-run-two-Python-pr ...
- OC + RAC (五) RACMulticastConnection
-(void)_test5{ //弊端:有几个订阅者就会请求几次数据 // 1.创建信号 RACSignal *signal = [RACSignal createSignal:^RACDisposa ...
- php中ajax的使用实例讲解
一.总结 1.多复习:代码都挺简单的,就是需要复习,要多看 2.ajax原理:ajax就是部分更新页面,其实还在的html页面监听到事件后,然后传给服务器进行操作,这里用的是get方式来传值到服务器, ...