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 ...
随机推荐
- Redis-Sentinel 哨兵
为什么需要哨兵? 一旦主节点宕机,那么需要人为修改所有应用方的主节点地址(改为新的master地址),还需要命令所有从节点复制新的主节点 那么这个问题,redis-sentinel就可以解决了 什么是 ...
- vscode插件篇
Document This 注释插件 能够自动识别function中的参数 Ctrl + alt + D
- 牛客网round1
题解: 1. 二分答案之后判断 把式子移项使得x,y不关联 #include <bits/stdc++.h> using namespace std; #define rint regis ...
- Web前端开发:Sublime Text 常用插件
在安装这些插件之前,确保你已经安装了Package Control. 安装Package Control方法: 通过菜单栏View->Show Console 或者快捷键Ctrl+` 打 ...
- datatable 转list ,list转datatable
方法一: public static IList<T> ConvertToModel(DataTable dt) { // 定义集合 ...
- Linux下的Mysql数据库备份+还原
数据库备份: root@debian-mm:/home/debian-mm# mysqldump -u root -p Account > Account.sql Enter password: ...
- Tp-validate进阶
阶段1:基础 application/controller/v1/Banner.php <?php namespace app\api\controller\v1; use think\Cont ...
- Codeforces 1073C Vasya and Robot 【二分】
<题目链接> 题目大意: 一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假 ...
- 初心不负 笔记-JS高级程序设计-引用类型篇-Array
ES3方法集合: 1join()方法,将一个数组里面的所有元素转换成字符串,然后再将他们连接起来返回一个字符串,通过制定的符号,默认值为逗号.不会改变原数组 ,,,,]; a.join(); &quo ...
- Linux系统开发之路-上
本节内容主要介绍Linux操作系统的主要特性,包括Linux与Windows操作系统的主要区别:Linux系统的分类:开发环境的推荐:Linux操作系统的安装:Linux系统下开发环境的安装和配置. ...