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. python全栈开发day80--评论楼、评论树

    内容总结: 1. 内容回顾 1. 内容回顾 1.评论 1. 展示评论 1. 评论楼(Django模板语言渲染) 1. 从后端查询出所有的评论 2. 如果有父评论就展示父评论 2. 评论树 通过ajax ...

  2. (一)cygwin和vim——hello world!

    好吧,我现在初出茅庐,一无所有,只有一台win xp.做什么呢?要不要试试Unix命令行编程的感觉,想到就做.Just try! 1.首先安装cygwin,最好是选择离线安装包. 2.默认选择安装所有 ...

  3. 通俗理解webService及.net中的使用方法

    什么是WebService? WebService两个关键字:HTTP.接口 WebService就是一个接口,与普通接口的区别就是:普通接口只能本地调用:WebService可以远程调用. WebS ...

  4. Python_tuple部分功能介绍

    x.count():元素在元组内的个数 x.index():元素在元组内的位置

  5. Centos7X部署Zabbix监控

    一:yum安装LAMP环境 zabbix-server端防火墙配置(可以选择iptables -F清空) iptables -A INPUT -m state --state NEW -m tcp - ...

  6. Python select解析

    一.首先列一下,sellect.poll.epoll三者的区别 1.select a.select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监控多个文件描述符的数组,当 ...

  7. BZOJ3295 [Cqoi2011]动态逆序对 分治 树状数组

    原文链接http://www.cnblogs.com/zhouzhendong/p/8678185.html 题目传送门 - BZOJ3295 题意 对于序列$A$,它的逆序对数定义为满足$i< ...

  8. 三级区域jquery插件

    /*! * Distpicker v1.0.4 * https://github.com/fengyuanchen/distpicker * * Copyright (c) 2014-2016 Fen ...

  9. IDEA设置

    一:代码提示 二:自动导入

  10. TF之RNN:TensorBoard可视化之基于顺序的RNN回归案例实现蓝色正弦虚线预测红色余弦实线—Jason niu

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt BATCH_START = 0 TIME_STEP ...