poj 2243 a星搜索
来源:互联网 发布:centos6配置本地yum源 编辑:程序博客网 时间:2024/05/18 01:02
a星算法我不介绍了,参考这个链接http://my.oschina.net/u/211101/blog/126259
直接上这个题目的代码:
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <cmath>using namespace std;int dir_x[] = {-2, -1, 1, 2, 2, 1, -1, -2};int dir_y[] = {1, 2, 2, 1, -1, -2, -2, -1};const int row = 8;const int col = 8;struct node{int x;int y;int step;int g, h, f;node(){}node(int x, int y, int step, int g, int h, int f):x(x), y(y), step(step), g(g), h(h), f(f){}bool operator < (const node &a) const{return f > a.f;}};bool vis[row + 2][col + 2];node start, end;bool judgeSide(const node &tmp) //判断tmp点是否在棋盘上面{return tmp.x >= 1 && tmp.x <= 8 && tmp.y >= 1 && tmp.y <= 8;}int heuristic(const int &x, const int &y) //启发值{return (abs(x - end.x) + abs(y - end.y)) * 10;}priority_queue<node> pq;int aStare(){node top, tmp;while(!pq.empty()){top = pq.top();pq.pop();vis[top.x][top.y] = true;if(top.x == end.x && top.y == end.y)return top.step;for(int i = 0; i < 8; i++){tmp.x = top.x + dir_x[i], tmp.y = top.y + dir_y[i], tmp.step = top.step + 1, tmp.g = top.g + 23, tmp.h = heuristic(tmp.x, tmp.y), tmp.f = tmp.g + tmp.h;if(judgeSide(tmp) && vis[tmp.x][tmp.y] == false){pq.push(tmp);}}}}int main(){char c1, c2;while(cin >> c1 >> start.y >> c2 >> end.y){memset(vis, false, sizeof(vis));while(!pq.empty())pq.pop();start.x = c1 - 'a' + 1;start.step = 0;start.g = 0, start.h = heuristic(start.x, start.y), start.f = start.g + start.h;end.x = c2 - 'a' + 1;pq.push(start);int res = aStare();cout << "To get from " << c1 << start.y << " to " << c2 << end.y << " takes " << res << " knight moves." << endl;}}
- poj 2243 a星搜索
- poj 2243(A*搜索orBFS)
- POJ 2599 A funny game (搜索,博弈)
- poj 3897 Maze Stretching 二分+A*搜索
- POJ-2415 Hike on a Graph 搜索
- Poj 2488 A Knight's Journey(搜索)
- POJ 2488 A Knight's Journey 搜索
- [POJ 2449] Remmarguts' Date [A*搜索]
- POJ 2488 A Knight's Journey 搜索
- POJ 2488 A Knight's Journey 搜索
- POJ Remmarguts' Date A*搜索+spfa
- poj 2243 BFS搜索
- A星搜索 (A* Search)
- POJ---2243 Knight Moves 使用A*算法的广度优先搜索
- 简单搜索poj 2243(水
- poj 2488 A Knight's Journey(深度优先搜索)
- POJ 1077 Eight 八数码 A*搜索算法
- POJ 2599 A funny game(博弈搜索)
- php 创建 xml 文件
- UVA 11922 Permutation Transformer(伸展树 Splay Tree)
- 几个常用JSON组件的性能测试
- 最长递增子序列 O(NlogN)算法 ( DP + 二分查找)
- 一个ForkJoin的示例
- poj 2243 a星搜索
- 第一个驱动 入口 卸载分析
- Matlab连接SQL数据库【完整版】【包括32位和64位】【自己手码的】
- Android入门之RatingBar(评分)
- 标准I/O的缓冲机制的实例讲解
- 解救小Q(bfs)
- ORACLE 11G利用ACS解决bind peeking带来的问题
- 最常用的Linux命令简表
- C++中的string类的用法总结