A-star 算法

算法证明略
做题步骤:
如果搜索空间过大,考虑使用A*算法,正常做真实距离,思考估价函数,验证估价函数的正确性,队列换成优先队列,新加一维 真实+估计,终点出队即为答案
AcWing 179. 八数码
题目链接
估价函数:当前状态种每个数与它的目标位置的曼哈顿距离之和
把数码存在解的条件:逆序对为偶数
估价函数涉及到曼哈顿距离的情况很多
代码:

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, string>PIS;
typedef pair<string, char>PSC;
int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
string op = {"ruld"};
string ed = {"12345678x"};
string ans;
int f(string s){int sum = 0;for (int i = 0; i < 9; i ++){if (s[i] == 'x') continue;int x = s[i] - '1';sum += abs(x / 3 - i / 3 ) + abs(x % 3 - i % 3);}return sum;
}
void bfs(string start){unordered_map<string, int>dis;unordered_map<string, PSC>pre;priority_queue<PIS, vector<PIS>, greater<PIS>>heap;heap.push({0 + f(start), start});//cout << f(start) <<endl;while (heap.size()){auto t = heap.top();heap.pop();string state = t.se;//cout << state << endl;if(state == ed) break;int idx;for (int i = 0; i < 9; i ++){if(state[i] == 'x') idx = i;}int x = idx / 3, y = idx % 3;for (int i = 0; i < 4; i ++){string now = state;int nx = x + dx[i], ny = y + dy[i];swap(now[idx], now[nx * 3 + ny]);if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue;if (dis[now] && dis[now] < dis[state] + 1) continue;dis[now] = dis[state] + 1;heap.push({dis[now] + f(now), now});pre[now] = {state, op[i]};}}while (ed != start){ans += pre[ed].se;ed = pre[ed].fi;}reverse(ans.begin(), ans.end());
}
int main(){string start, s;string c;while (cin >> c){start += c;if (c != "x") s += c;}int cnt = 0;for (int i = 0; i < 8; i ++){for (int j = i; j < 8; j ++){if (s[j] < s[i]) cnt++;}}if (cnt & 1) cout << "unsolvable" << endl;else {bfs(start);cout << ans <<endl;}return 0;
}

178. 第K短路
题目链接
估价函数:每个点到终点的最短距离(反向跑一边dij)
第k短路 -> 弹出顺序
注意起点和终点相同的情况
代码:

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int>PII;
typedef pair<int, PII>PIII;
const int N = 2e5+10;
int h[N], rh[N], e[N], ne[N], w[N], idx;
int n, m;
int s, ed, k;
int dis[N], cnt[N];
bool st[N];
void add(int h[], int a, int b, int c){e[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx++;
}
void dij(){memset(dis, 0x3f, sizeof(dis));dis[ed] = 0;priority_queue<PII, vector<PII>, greater<PII>>heap;heap.push({0,ed});while (heap.size()){auto t = heap.top();heap.pop();int u = t.se;if (st[u]) continue;st[u] = true;for (int i = rh[u]; i != -1; i = ne[i]){int j = e[i];if (dis[j] > t.fi + w[i]){dis[j] = t.fi + w[i];heap.push({dis[j], j});}}}
}
int Astar(){priority_queue<PIII, vector<PIII>, greater<PIII>>heap;heap.push({dis[s], {0, s}});while (heap.size()){auto t = heap.top();heap.pop();int u = t.se.se, distance = t.se.fi;cnt[u]++;if (cnt[ed] == k) return distance;for (int i = h[u]; i != -1; i = ne[i]){int j = e[i];if (cnt[j] >= k) continue;//提前剪枝,减少搜索数量heap.push({distance + w[i] +dis[j], {distance + w[i], j}});}}return -1;
}
int main(){memset(rh, -1, sizeof(rh));memset(h, -1, sizeof(h));cin >> n >> m;for(int i = 0; i < m; i ++){int a, b, c;cin>> a >> b >>c;add(h, a, b, c);add(rh, b, a, c);//建反图}cin >> s >> ed >> k;if(s == ed) k ++;//特判起点和终点相同的情况dij();cout << Astar() << endl;return 0;
}

AcWing 179. 八数码 178. 第K短路 (A-star)相关推荐

  1. 算法提高课-搜索-A*(A star)算法-AcWing 179. 八数码:A星算法求解

    题目分析 来源:acwing 分析: A*算法是什么呢? A算法是一种bfs的变式,需要用到一个"估价函数",用来计算任意状态到目标状态所需代价的估计值.然后在搜索中,维护一个堆, ...

  2. AcWing 845. 八数码(3阶数字华容道):bfs求最短路,状态表示困难

    文章目录 题目 题目分析 题目 题目链接:AcWing 845. 八数码(数字华容道) 在一个3×3的网格中,1~8这8个数字和一个"x"恰好不重不漏地分布在这3×3的网格中. 例 ...

  3. 178 第K短路(A*算法优化)

    1. 问题描述: 给定一张 N 个点(编号 1,2-N),M 条边的有向图,求从起点 S 到终点 T 的第 K 短路的长度,路径允许重复经过点或边.注意: 每条最短路中至少要包含一条边. 输入格式 第 ...

  4. AcWing 845. 八数码

    这道题如同难度一样,确实不好上手,既然求最短路径,直接bfs就好,但是bfs的核心就是标记,我们该怎么标记一个字符串呢,我们发现这个字符串很特殊,我们标记的只有这个字符串的全排列,然后我也是学到了一个 ...

  5. 爬山法、随机重启爬山法、模拟退火算法对八皇后问题和八数码问题的性能测试...

    代码地址:https://github.com/laiy/AI/tree/master/awesome-search 一些前提: 1. 首先要明确这些算法并不是用于解决传统的搜索问题的(环境是可观察的 ...

  6. hdu 1043 ,pku 1077 Eight ,八数码问题

    某位神牛曾说过,此题是涉及到人生完不完整的一道题.. Goodness大牛曾总结了 八数码的八重境界 : http://www.cnblogs.com/goodness/archive/2010/05 ...

  7. HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法

    先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...

  8. Eight HDU - 1043(八数码+搜索)

    题意: 就是还原八数码.输出操作. 题目: The 15-puzzle has been around for over 100 years; even if you don't know it by ...

  9. 八皇后问题和八数码问题的最陡上升爬山法、首选爬山法、随机重启爬山法、模拟退火算法的分析和实现

    对经典算法的问题的回顾与感想 对八皇后问题和八数码问题分别用最陡上升爬山法.首选爬山法.随机重启爬山法.模拟退火算法来实现,并且分析他们的性能. 分析 要求实现的各个算法是有共同点的,比如,八皇后问题 ...

最新文章

  1. 寒武纪开盘暴涨350%,市值突破1000亿,85后创始人身家超300亿!千亿盛宴背后隐忧不可忽视!...
  2. linux中 用户管理命令,Linux中的常用用户和用户组管理命令
  3. H3C LMI协议标准
  4. mysql程序设计教程_MySQL教程_编程入门教程_牛客网
  5. ubuntu同时装有MXNet和Caffe框架
  6. java知识点3(null、引用相关知识(自己理解))
  7. orm2 中文文档 3. 定义模型
  8. 现在ui设计出来好找工作吗?
  9. 2018-08-10 Netty:4.x
  10. Uniwebview2插件常见问题以及刘海屏屏幕适配,屏幕旋转的解决方案
  11. 西门子estop指令_西门子6RA80直流调速器调试步骤和参数设置
  12. mac 4k分辨率 字太小 27寸 hidpi_2019年两千价位你可以买到一台怎样的4K显示器?AOC U2790PQU...
  13. 看操作系统是x84还是x64啊
  14. 2019届大疆提前批校招机器学习岗笔试B卷
  15. 如何找回u盘被删除的文件
  16. Linux实战教学笔记28:企业级LNMP环境应用实践
  17. 基于URL特征的网站结构信息挖掘
  18. 【佳学基因人工智能】在ANACOND3下如何安装NUMPY
  19. 软件项目方案模板~!
  20. ImportError: packaging>=20.0 is required for a normal functioning of this mo

热门文章

  1. 云顶之弈机器人法爆_云顶之弈:6法机器人主C打法来了,一钩3400,3星效果最好!...
  2. 用python解决百马百担问题_利用C语言实现“百马百担”问题方法示例
  3. 网易游戏(雷火、盘古、伏羲)9月线下笔试真题(游戏开发方向)
  4. 我的个性签名--生活记录
  5. 微信昵称 知识从未如此性感_为什么开源从未如此强大
  6. 爬取混合类基金收益率与其股票持有情况
  7. 竞选计算机课代表演讲稿开头,竞选课代表的演讲稿模板集锦8篇
  8. 关于windows系统安装时提示不能装在GPT分区格式的分区上的解决办法
  9. Windows动态链接库(dll)浅析 - 2
  10. linux脚本用户输入,如何在Linux shell脚本中提示用户输入