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+康托展开)的更多相关文章

  1. HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】

    一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...

  2. loj 1165(bfs+康托展开)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...

  3. hdu1430魔板(BFS+康托展开)

    做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...

  4. 蓝桥杯 历届试题 九宫重排 (bfs+康托展开去重优化)

    Description 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的 ...

  5. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  6. hdu 1430(BFS+康托展开+映射+输出路径)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  7. HDU_1430 魔板 【BFS+康托展开+置换】

    一.题面 POJ1430 二.分析 该题与之前做的八数码不同,它是一个2*4的棋盘,并且没有空的区域.这样考虑的情况是很少的,依然结合康托展开,这时康托展开最多也只乘7的阶乘,完全可以BFS先预处理一 ...

  8. HDU - 1430 魔板 【BFS + 康托展开 + 哈希】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...

  9. hdu 5012 bfs 康托展开

    Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

随机推荐

  1. bzoj4520【CQOI2016】K远点对

    题解: kd-tree裸题 对每个点维护最近的k个开个堆维护一下

  2. [转]DBCP连接池的最简单应用(用于ORACLE数据库)

    http://blog.csdn.net/iihero/article/details/8254107 http://www.programgo.com/article/81693457907/ 鉴于 ...

  3. PrintDocument打印、预览、打印机设置和打印属性的方法

    WindowsForm 使用 PrintDocument打印.预览.打印机设置和打印属性的方法. private void Form1_Load(object sender, System.Event ...

  4. websocket/dwebsocket 实现前后端的实时通信

    1.  用bottle框架,自己写一个服务端实现: 转载   :http://www.linuxyw.com/813.html 功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上 ...

  5. 完成将 toChineseNum, 可以将数字转换成中文大写的表示,处理到万级别,例如 toChineseNum(12345),返回 一万二千三百四十五

    const toChineseNum = (num) => { const unit = ['', '十', '百', '千'] const counts = ['零', '一', '二', ' ...

  6. Codeforces 781D Axel and Marston in Bitland 矩阵 bitset

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF781D.html 题目传送门 - CF781D 题意 有一个 n 个点的图,有 m 条有向边,边有两种类型: ...

  7. Codechef EDGEST 树套树 树状数组 线段树 LCA 卡常

    原文链接http://www.cnblogs.com/zhouzhendong/p/9016579.html 题目传送门 - Codechef EDGEST 题意 给定相同点集上的两棵生成树$T_1$ ...

  8. 20165235 祁瑛 Java第四周考试总结

    20165235 祁瑛 Java第四周考试总结 课后习题p29 p45 代码编写 import java.util.*; class Example2_5{ public static void ma ...

  9. mybatis提示Invalid bound statement (not found)错误的可能原因

    https://www.cnblogs.com/liaojie970/p/8034525.html

  10. Java中的不同遍历方式

    已知一个Person类: public class Person implements Comparable<Person>{ String name; String id; public ...