题目链接

题意

中文题意

思路

做这题的前置技能学习

  1. 康托展开

    这个东西我认为就是在排列组合问题上的Hash算法,可以压缩空间。

  2. A*搜索。

    这里我使用了像k短路一样的做法,从最终状态倒回去预处理一遍距离,但是跑了0.8s,可能是预处理花费的时间太多了。有些人用曼哈顿距离估价,跑了0.2s。

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
typedef long long LL;
struct Node {
int x, y, f, g, kt, mm[4][4];
bool operator < (const Node &rhs) const {
return f > rhs.f;
}
};
int init[4][4] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 0 }
};
int mp[4][4], ans, h[500001], vis[500001], target;
int f[10], dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; int kangtuo(int mp[4][4]) {
int sum = 0;
for(int i = 0; i < 9; i++) {
int now = 0;
for(int j = i + 1; j < 9; j++) {
if(mp[i/3][i%3] > mp[j/3][j%3]) now++;
}
sum += now * f[9-i-1];
} return sum + 1;
} void BFS(int x, int y) {
memset(h, INF, sizeof(h));
queue<Node> que;
Node now = (Node) {x, y, 0, 0};
for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++)
now.mm[i][j] = init[i][j];
h[target] = 0;
que.push(now);
while(!que.empty()) {
Node now = que.front(); que.pop();
int x = now.x, y = now.y, f = now.f;
for(int i = 0; i < 4; i++) {
now.x = x + dx[i], now.y = y + dy[i];
if(now.x < 0 || now.x > 2 || now.y < 0 || now.y > 2) continue;
swap(now.mm[x][y], now.mm[now.x][now.y]);
int nf = f + 1, nkt = kangtuo(now.mm);
now.f = nf;
if(h[nkt] == INF) {
h[nkt] = nf;
que.push(now);
}
swap(now.mm[x][y], now.mm[now.x][now.y]);
}
}
} void YCL() {
BFS(2, 2);
} int Astar(int x, int y) {
priority_queue<Node> que;
memset(vis, 0, sizeof(vis));
vis[kangtuo(mp)] = 1;
Node now = (Node) { x, y, 0, 0, kangtuo(mp)};
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
now.mm[i][j] = mp[i][j];
que.push(now);
while(!que.empty()) {
now = que.top(); que.pop();
int x = now.x, y = now.y, f = now.f, g = now.g, kt = now.kt;
if(kt == target) return f;
for(int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if(nx < 0 || nx > 2 || ny < 0 || ny > 2) continue;
swap(now.mm[x][y], now.mm[nx][ny]);
int nkt = kangtuo(now.mm);
now.x = nx, now.y = ny, now.g = g + 1, now.f = now.g + h[nkt], now.kt = nkt;
if(!vis[nkt]) {
vis[nkt] = 1;
que.push(now);
}
swap(now.mm[x][y], now.mm[nx][ny]);
}
} return -1;
} int main() {
f[0] = 1;
for(int i = 1; i < 9; i++) f[i] = f[i-1] * (i);
target = kangtuo(init);
YCL();
int t; scanf("%d", &t);
while(t--) {
int ix, iy;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++) {
scanf("%d", &mp[i][j]);
if(mp[i][j] == 0) ix = i, iy = j;
}
ans = Astar(ix, iy);
if(~ans) printf("%d\n", ans);
else puts("No Solution!");
}
return 0;
}

hihoCoder 1312:搜索三·启发式搜索(A* + 康托展开)的更多相关文章

  1. hihoCoder #1312 : 搜索三·启发式搜索(A*, 康托展开)

    原题网址:http://hihocoder.com/problemset/problem/1312 时间限制:10000ms 单点时限:1000ms 内存限制:256MB   描述 在小Ho的手机上有 ...

  2. 【hihocoder 1312】搜索三·启发式搜索(启发式搜索写法)

    [题目链接]:http://hihocoder.com/problemset/problem/1312?sid=1092363 [题意] [题解] 定义一个A*函数 f = step+val 这里的v ...

  3. 【hihocoder 1312】搜索三·启发式搜索(普通广搜做法)

    [题目链接]:http://hihocoder.com/problemset/problem/1312?sid=1092352 [题意] [题解] 从末状态的123456780开始逆向搜; 看它能到达 ...

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

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

  5. 康托展开:对全排列的HASH和还原,判断搜索中的某个排列是否出现过

    题目:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2297 前置技能:(千万注意是 ...

  6. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

  7. HDU 1430 魔板(康托展开+BFS+预处理)

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

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

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

  9. poj1077(康托展开+bfs+记忆路径)

    题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...

随机推荐

  1. POJ读书笔记2.1 —— 鸡兔笼带

    http://blog.csdn.net/pipisorry/article/details/36433305 问题描写叙述  一个笼子里面关了鸡和兔子(鸡有2仅仅脚.兔子有4仅仅脚.没有例外). 已 ...

  2. Java数据结构和算法的数组

    阵列的功能: 1.固定大小 2.相同的数据类型 3. 4.数据项可反复 Java数据类型:基本类型(int和double)和对象类型.在很多编程语言中.数组也是基本类型.但在Java中把它们当作对象来 ...

  3. XF 进度条和指示器

    <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http:/ ...

  4. DataGridView 鼠标双击获得行列索引

    如果是CellDoubleClick事件,可以在事件的参数e里获取列索引及行索引.也可以通过CurrentCell来获取当前选中的单元格的列索引及行索引.参考:private void dataGri ...

  5. AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享

    原文:AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享 ====================www.ayjs.net       杨洋    wpfui.com      ...

  6. 16.09 working note

    这个月最主要任务是linux shell script学习. 其次是继续spring源码学习. 其余时间C.C++和Java学习. 01 9月第一天,9点多才到家.做道简单的oj题练习下.因为简单,所 ...

  7. VPS用来配置上网外,还可以做一个同步盘

    我曾经在一个活动的博文里说过,男人必须要有一个VPS和一个树莓派,VPS这个东西,以后会是中国男人的一种必备技能,今天又有一个小伙伴请教我VPS的用法,我就简单说说我目前使用的情况.首先我希望你能有点 ...

  8. CSS3 GENERATOR可以同时为一个元素完成border-radius、box-shadow、gradient和opacity多项属性的设置

    CSS3 GENERATOR可以同时为一个元素完成border-radius.box-shadow.gradient和opacity多项属性的设置 CSS3 GENERATOR 彩蛋爆料直击现场 CS ...

  9. CrashRpt_v.1.4.2_vs2008_also_ok

    1.windows多线程程序release版崩溃记录工具,便于该如何查找错误. 2.此工具主要用来配置windbug工具,一种排查程序发布版本崩溃这种非常难处理的缺陷的方法,非常棒,amazing! ...

  10. js一道面试题

    题目是这样的,按照以下函数的结果写一个sub()方法: sub(1)(2)(3);/*return 6*/ sub(4)(3)(3);/*return 10*/ 我的山寨做法: function su ...