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 ...
随机推荐
- [转]利用ssh传输文件
利用ssh传输文件 http://www.cnblogs.com/jiangyao/archive/2011/01/26/1945570.html 在linux下一般用scp这个命令来通过ssh传输文 ...
- mysql基本操作(二)
1.向表msg中插入数据,先创建表,再插入数据 mysql> create table msg ( -> id int, -> title varchar(60), -> na ...
- ftp弱密码案例
- Django项目和Django初体验和创建、目录结构认识
.MVC的设计方式(跟Flask一样,都是MVC的设计模式) .开发效率高 .功能强大(丰富的第三方组件) .安全性高(帮助开发者规避安全漏洞) 目前市面上使用:Django>Flask #使用 ...
- servlet生命周期和工作原理
http://www.cnblogs.com/cuiliang/archive/2011/10/21/2220671.html
- img-html-2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用sparksql往kafka推送数据
一.相关配置参数 1.同级目录resource文件夹下配置 brokers_list=kafkaxxx02broker01:9092,kafkaxxx02broker02:9092,kafkaxxx0 ...
- Python常用模块--collections
collections是Python中一个非常强大的容器数据模块. 1.创建升级版的元组--namedtupe Python的元组(1,2,3)具有不可变性,但是单独的元组在无法满足现有需求时,可以使 ...
- 手动实现Promise
Promise对大家来说并不是很陌生,它是一个异步编程的解决方案,主要解决了前端回调地域问题.用阮老师的话说,它“就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果”. Pro ...
- input模拟输入下拉框
功能点: 输入.下拉选择.根据输入内容模糊检索.键盘上下键选择 实现思路: 显示隐藏: input获取焦点显示,失去焦点隐藏 下拉选择: 以父元素为基准,通过绝对定位定位至input输入下方 模 ...