题目描述

Scenario

The 15-puzzle has been around for over 100 years; even if you don’t know it by that name, you’ve seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let’s call the missing tile ‘x’; the object of the puzzle is to arrange the tiles so that they are ordered as:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 x

where the only legal operation is to exchange ‘x’ with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:

1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->

The letters in the previous row indicate which neighbor of the ‘x’ tile is swapped with the ‘x’ tile at each step; legal values are ‘r’,’l’,’u’ and ‘d’, for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing ‘x’ tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.

题目分析

就是根据每两个位置之间的曼哈顿距离作为估值函数。。。然后用康拓展开判重。

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <iostream>
#include <string>
using namespace std;
const int MAXVIS = 363900;
struct State{unsigned short s[9];int h, g, mh;bool operator < (const State& s) const {return (g+h)>(s.g+s.h);}bool operator > (const State& s) const {if((g+h) == (s.g+s.h))return g < (s.g+s.h);return (g+h)<(s.g+s.h);}
}st, ed;
priority_queue<State> que;
char now[MAXVIS+5]; int pre[MAXVIS+5];
int _pow[9]; pair<int, int> show[9];
int fx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int ppos[9][2] = {{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
int fppos[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
char ch[4] = {'d', 'r', 'u', 'l'};
int vis[MAXVIS+5];
int _hash(unsigned short s[]){int ret = 0, count =0 ;for(int i=0;i<9;i++){count = 0;for(int j=i+1;j<9;j++)count += int(s[j]<s[i]);ret += count * _pow[8-i];}return ret;
}
int _abs(int u){return u>0?u:-u;}
int _h(unsigned short s[]){int ret = 0;for(int i=0;i<9;i++) if(s[i])ret += _abs(ppos[i][0]-show[s[i]].first) + _abs(ppos[i][1]-show[s[i]].second);return ret;
}
bool check_pos(const int& a, const int& b){if(a > 2 || b > 2 || a < 0 || b < 0) return false;return true;
}
stack<char> sta;
void Print(){while(!sta.empty()) sta.pop();int nowm = ed.mh;while(nowm != st.mh) {sta.push(now[nowm]);nowm = pre[nowm];}while(!sta.empty()){putchar(sta.top());sta.pop();}puts("");
}
void solve(){int counter = 0;for(int i=0;i<9;i++) if(st.s[i]){for(int j=i+1;j<9;j++) if(st.s[j]){if(st.s[i] > st.s[j]) counter++;}}if(counter & 1){cout<<"unsolvable"<<endl;return ;}memset(vis, 0x3f, sizeof vis);while(!que.empty()) que.pop();State tmp=st; State t2;now[tmp.mh] = 0;int pos;vis[tmp.mh] = tmp.h = _h(st.s); tmp.g = 0;que.push(tmp);while(!que.empty()){tmp = que.top(); que.pop();pos=0;if(tmp.mh == ed.mh){Print();return ;}if(tmp.g + tmp.h > vis[tmp.mh]) continue;while(tmp.s[pos]) pos++;for(int i=0;i<4;i++) {if(check_pos(ppos[pos][0] + fx[i][0], ppos[pos][1] + fx[i][1])){t2 = tmp;swap(t2.s[pos], t2.s[fppos[ppos[pos][0] + fx[i][0]][ppos[pos][1] + fx[i][1]]]);t2.mh = _hash(t2.s); t2.h = _h(t2.s); (++t2.g);if(t2.g + t2.h > vis[t2.mh]) continue;vis[t2.mh] = t2.g + t2.h;now[t2.mh] = ch[i]; pre[t2.mh] = tmp.mh;que.push(t2);}}}cout<<"unsolvable"<<endl;
}
int main(){char tstr[5];_pow[0] = 1;for(int i=1;i<=8;i++) _pow[i] = _pow[i-1] * i;ed.s[0] = 1; ed.s[1] = 2; ed.s[2] = 3; ed.s[3] = 4;ed.s[4] = 5; ed.s[5] = 6; ed.s[6] = 7; ed.s[7] = 8;ed.s[8] = 0;ed.mh = _hash(ed.s);for(int i=0;i<9;i++){show[ed.s[i]] = make_pair(i/3, i%3);}while(true){for(int i=0;i<9;i++){if(scanf("%s", tstr) == EOF) return 0;if(tstr[0] != 'x')st.s[i]=tstr[0]-'0';else st.s[i] = 0;}st.mh = _hash(st.s);if(st.mh == ed.mh) puts("");else solve();}return 0;
}

转载于:https://www.cnblogs.com/JeremyGJY/p/5921628.html

【启发式搜索】[ZOJ1217]Eight相关推荐

  1. [启发式搜索/A*] [SCOI2005]骑士精神题解

    洛谷-骑士精神 启发式搜索-A* 估价函数 对于当前状态,我们可以将其与目标状态对比,得到一个预估的代价,即最少(不一定满足题意)的代价,得到这个代价的函数叫做估价函数 对于一个最短路问题来说,我们可 ...

  2. GMIS 2017 Martin Müller演讲:深度学习时代的启发式搜索

    GMIS 2017 Martin Müller演讲:深度学习时代的启发式搜索 2017-05-28 14:34:43     GMIS 2017    0 0 0 5 月 28 日,机器之心全球机器智 ...

  3. 【机器学习】信用卡欺诈检测|用启发式搜索优化XGBoost超参数

    本文将展示如何使用模拟退火[1]启发式搜索[2]机器学习算法中超参数的最佳组合.这些方法比盲随机生成参数得到的模型效果好.另外,模型效果最好是分别微调每个超参数,因为它们之间通常存在交互. 模拟退火简 ...

  4. 启发式搜索给神经网络_神经科学如何支持UX启发式

    启发式搜索给神经网络 重点 (Top highlight) Interaction and UX designers have long known and used heuristics to gu ...

  5. 启发式搜索 迭代加深搜索 搜索对象的压缩存储

    常见的几种搜索算法 常见的几种搜索算法_唐宋缘明卿_cris的博客-CSDN博客_搜索算法有哪些 搜索 -- 启发式搜索 搜索 -- 启发式搜索_Alex_McAvoy的博客-CSDN博客_启发式搜索 ...

  6. 搜索 —— 启发式搜索

    [概述] 启发式搜索算法,就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标. 有时我们会遇到这样的一类题:题目描述的是一道时间复杂度很高的 NP 问题,我 ...

  7. 【图论】【启发式搜索】【二分查找】[POJ 3897]Maze Stretching

    启发式搜索主要就是加速,其实不用也可以过.二分L然后每次看最短路是多少,估价函数就是曼哈顿距离,然后注意精度误差就好了Tip:在POJ上交这道题目记得用c++千万不要用g++ #include < ...

  8. 【BZOJ1085】迭代加深+启发式搜索

    分析 跳马,首先跳马写起来就很复杂了.我们跳空格就行了. 然后,我一看这个题,15步以上就算-1,那好啊,直接写了个爆搜. 结果样例都跑不出来.. 遂考虑启发式搜索. 评估函数很显然,现在有多少个没归 ...

  9. 人工智能-搜索----启发式搜索

    搜索算法的形式化描述: <状态state.动作motion.状态转移state transition.路径path.测试目标test target> 一.启发式搜索(有信息搜索)(Heur ...

最新文章

  1. hls二次加密 m3u8_HLS实战之Wireshark抓包分析
  2. java访问器_Java中的访问器方法
  3. 【Cannot convert from [[B] to】 @RabbitListener 反序列化报错
  4. 华为rh5885服务器oid_华为RH5885H v3机架服务器RAID配置实例
  5. 使用政府开放数据和低代码方案构建应用
  6. composer设置代理_composer 设置代理
  7. linux云服务器 个人,使用ownCloud在Linux安装你的个人云服务
  8. 单片机c语言出租车计时程序,基于单片机的出租车计费(c语言
  9. win11 windows 服务打开word 另存为pdf
  10. ZInt支持中文例子
  11. 大学英语(第四册)复习(原文及全文翻译)——Unit 5 - TO LIE OR NOT TOLIE—THE DOCTOR‘S DILEMMA(撒谎还是不撒谎——医生的难题)
  12. 库存管理系统的设计与实现(代码)
  13. Entry name ‘META-INF/xxx‘ collided报错
  14. 清空el-form表单数据(整理)
  15. 简历中的工作经历要怎么写?
  16. 在matlab中使用dsolve函数解范德波尔二阶微分方程
  17. 2021贵港市地区高考成绩排名查询,贵港高中成绩排名2021,贵港中考分数线排行榜...
  18. html插音乐的步骤,HTML插入背景音乐方法【全】
  19. uni-app登录界面案例
  20. matlab伪随机数(以及如何得到真正的随机数)

热门文章

  1. [转]七大.NET开源框架
  2. 《LeetCode力扣练习》第141题 环形链表 Java
  3. mysql userstat_mysql 中记录用户登录错误日志方法小结
  4. flutter图片识别_Flutter 资源和图片
  5. python代码优化_Python 代码优化技巧(一)
  6. mysql repalication_mysql replication(主从复制)(一)MS模式
  7. Matlab中与复数有关的函数abs()、angle()
  8. matlab创建mat格式变量并导入数据
  9. okhttp 工具类_HR常用的人才测评工具 ~ 团测系统
  10. sizeof()与strlen()的区别与联系