Aizu0121 Seven Puzzle(bfs+康托展开)
https://vjudge.net/problem/Aizu-0121
比八数码要水的多,bfs。
但是做的时候我把康托展开记错了,wa了好几次。
附上康托展开博客详解:https://blog.csdn.net/wbin233/article/details/72998375
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int vis[], b[];
int dir[][] = {, , -, , , , , -};
int f[] = {, , , , , , , };
typedef struct{
int a[][];
int step;
}Node;
Node node;
int kantor(int a[][])//求法记错了!!
{
int k=, sum=, t;
for(int i = ; i < ; i++){
for(int j = ;j < ; j++){
b[k++] = a[i][j];
}
}
for(int i = ; i < ; i++){
t = ;
for(int j = i+; j < ; j++){
if(b[i]>b[j])
t++;
}
sum += t*f[-i-];
}
return sum;
}
int panduan(int a[][])
{
int k = ;
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
if(a[i][j] != k++){
return ;
}
}
}
return ;
}
void bfs()
{
int x, y;
queue<Node> q;
node.step = ;
q.push(node);
int tmp = kantor(node.a);
vis[tmp] = ;
while(!q.empty()){
Node t = q.front(), p;
if(panduan(t.a)){
cout << t.step << endl;
break;
} for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
if(t.a[i][j] == ){
x = i; y = j; //0的位置
break;
}
}
}
p = t;
for(int i = ; i < ; i++){
int tx = x+dir[i][];
int ty = y+dir[i][];
if(tx>=&&tx<&&ty>=&&ty<){
swap(p.a[tx][ty], p.a[x][y]);
tmp = kantor(p.a);
if(!vis[tmp]){
vis[tmp] = ;
p.step++;
q.push(p);
p.step--;
}
swap(p.a[tx][ty], p.a[x][y]);
}
}
q.pop();
}
}
int main()
{
while(cin >> node.a[][]){
memset(vis, , sizeof(vis));
for(int i = ; i < ; i++){
cin >> node.a[][i];
}
for(int i = ; i < ; i++){
cin >> node.a[][i];
}
bfs();
}
return ;
}
Aizu0121 Seven Puzzle(bfs+康托展开)的更多相关文章
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
- loj 1165(bfs+康托展开)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- 蓝桥杯 历届试题 九宫重排 (bfs+康托展开去重优化)
Description 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- hdu 1430(BFS+康托展开+映射+输出路径)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HDU_1430 魔板 【BFS+康托展开+置换】
一.题面 POJ1430 二.分析 该题与之前做的八数码不同,它是一个2*4的棋盘,并且没有空的区域.这样考虑的情况是很少的,依然结合康托展开,这时康托展开最多也只乘7的阶乘,完全可以BFS先预处理一 ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- hdu 5012 bfs 康托展开
Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
随机推荐
- sendEmail 阿里云使用587端口
使用sendEmail使用参数 -o tls=yes -s smtp服务器+端口 因为阿里云屏蔽了25端口,我使用465也不成功,只有使用587端口,我使用的qq企业邮箱,测试可用
- sed 简单修改配置文件ip地址
sed -i 's/old ip/new ip/g' file.txt
- Application Initialization UI for IIS 7.5
IIS Application Initialization for IIS 7.5 enables website administrators to improve the responsiven ...
- Flink--输入数据集Data Sources
flink在批处理中常见的source flink在批处理中常见的source主要有两大类. 1.基于本地集合的source(Collection-based-source) 2.基于文件的sourc ...
- Kafka/Zookeeper集群的实现(二)
[root@kafkazk1 ~]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12. ...
- python 保存小数位,控制保存几位
不知道怎么展示浮点数长度?? 看看例子就清楚了 a=0.2343545434564 print('%.3f'%a) #加点保留X个print('%3f'%a) #默认保留小数6个print('%03f ...
- Codeforces Gym100783H 最短路 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100783H.html 题目传送门 - CF-Gym100783H 题意 给定一个 $n$ 个节点 ...
- js下拉列表
js清除下拉列表所选默认值 $("#lineId").val(“”); js清除下拉列表所有选项 $("#type").html(""); ...
- STC15W408AS简单使用教程-简单的光强检测!
第一步:搭建开发环境 安装最新版本的STC_ISP程序烧录软件,链接:http://pan.baidu.com/s/1slLPnOD 密码:6bov 安装keil C51的51系列单片机集成IDE软件 ...
- 爬虫3 requests基础之下载图片用content(二进制内容)
res = requests.get('http://soso3.gtimg.cn/sosopic/0/11129365531347748413/640') # print(res.content) ...