[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 ...
随机推荐
- Q开头的类找不到,无法加载插件:com.mysema.maven:apt-maven-plugin
http://www.jspxcms.com/documentation/297.html 如果出现无法加载com.mysema.maven:apt-maven-plugin插件的情况,通常是由于ma ...
- [POI2008]Sta(树形dp)
[POI2008]Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面 ...
- objectMaaper 反序列化json字段多于或少于实体处理
两种方式 1 忽略json上未知的字段 设置实体类注解 @JsonIgnoreProperties(ignoreUnknown = true) public class Foo { ... } 2 配 ...
- alert(1) to win 14
<!--<script></script>之间的内容会被当作js处理,所以,//we'll use this later </script>被注释了.最终 i ...
- 八个JS中你见过的类型。
1.布尔类型 布尔值只能为 true 或者 false ,其他的会报错 let bool: boolean = false; bool = true; // bool = 123; // error ...
- Nginx+lua_Nginx+GraphicsMagick来实现实时缩略图
1.安装GraphicsMagick cd /usr/local/src wget http://sourceforge.net/projects/graphicsmagick/files/graph ...
- Vue的自定义滚动,我用el-scrollbar
弄了一个持续更新的github笔记,可以去看看,诚意之作(本来就是写给自己看的--)链接地址:Front-End-Basics 此篇文章的地址:Vue的自定义滚动,我用el-scrollbar 基础笔 ...
- man LVCREATE
LVCREATE(8) LVCREATE(8) NAME/名称 lvcreat ...
- LeetCode--043--字符串相乘(java)
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...
- Telegraf根据配置文件启动(Influxdb的数据收集)
1.创建一个telegraf.config文件 telegraf -sample-config -input-filter cpu:disk:diskio:net:system:mem -output ...