POJ2243 Knight Moves —— A*算法
题目链接:http://poj.org/problem?id=2243
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14500 | Accepted: 8108 |
Description
the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
Output
Sample Input
- e2 e4
- a1 b2
- b2 c3
- a1 h8
- a1 h7
- h8 a1
- b1 c3
- f6 f6
Sample Output
- To get from e2 to e4 takes 2 knight moves.
- To get from a1 to b2 takes 4 knight moves.
- To get from b2 to c3 takes 2 knight moves.
- To get from a1 to h8 takes 6 knight moves.
- To get from a1 to h7 takes 5 knight moves.
- To get from h8 to a1 takes 6 knight moves.
- To get from b1 to c3 takes 1 knight moves.
- To get from f6 to f6 takes 0 knight moves.
Source
题解:
这题直接用BFS就可以过了。但是想借助这题学一下A*算法(但以下写法好像不是完整的或者说正确的写法,先放一放,以后遇到再深入)。
代码如下:
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <queue>
- #include <cmath>
- using namespace std;
- typedef long long LL;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e9+;
- const int MAXN = +;
- struct node
- {
- int x, y, step;
- int g, h, f;
- bool operator<(const node&a)const{
- return f>a.f;
- }
- };
- bool vis[MAXN][MAXN];
- int dir[][] = { {,}, {,-},{-,},{-,-},{,},{,-},{-,},{-,-} };
- int gdis(int x_step, int y_step) //直线距离
- {
- return (int)sqrt(x_step*x_step+y_step*y_step)*+;
- }
- int hdis(node a, node b) //曼哈顿距离
- {
- return ( abs(a.x-b.x) + abs(a.y-b.y) )*;
- }
- priority_queue<node>que;
- int Astar(node st, node en)
- {
- memset(vis,false,sizeof(vis));
- st.step = st.g = st.h = st.f = ;
- while(!que.empty()) que.pop();
- que.push(st);
- vis[st.x][st.y] = true;
- while(!que.empty())
- {
- node now = que.top();
- que.pop();
- if(now.x==en.x && now.y==en.y)
- return now.step;
- for(int i = ; i<; i++)
- {
- node tmp;
- tmp.x = now.x + dir[i][];
- tmp.y = now.y + dir[i][];
- if(tmp.x>= && tmp.x<= && tmp.y>= && tmp.y<= && !vis[tmp.x][tmp.y])
- {
- tmp.step = now.step + ;
- tmp.g = now.g + gdis(abs(dir[i][]), abs(dir[i][]));
- tmp.h = hdis(tmp, en);
- tmp.f = tmp.g + tmp.h;
- vis[tmp.x][tmp.y] = true;
- que.push(tmp);
- }
- }
- }
- }
- int main()
- {
- char s1[], s2[];
- while(scanf("%s%s", s1, s2)!=EOF)
- {
- node st, en;
- st.x = s1[]-'a'+;
- st.y = s1[]-'';
- en.x = s2[]-'a'+;
- en.y = s2[]-'';
- int step = Astar(st,en);
- printf("To get from %c%d to %c%d takes %d knight moves.\n",st.x+'a'-,st.y,en.x+'a'-,en.y,step);
- }
- }
POJ2243 Knight Moves —— A*算法的更多相关文章
- POJ---2243 Knight Moves 使用A*算法的广度优先搜索
题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- 跳马(Knight Moves), ZOJ1091, POJ2243 x
跳马(Knight Moves), ZOJ1091, POJ2243 题目描述: 给定象棋棋盘上两个位置 a 和 b,编写程序,计算马从位置 a 跳到位置 b 所需步数的最小值. 输入描述: 输入文件 ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- HDU 1372 Knight Moves 题解
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Knight Moves (双向bfs)
# 10028. 「一本通 1.4 例 3」Knight Moves [题目描述] 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数.骑士一步可以移动到的位置由下图给出. [算法 ...
- Knight Moves UVA - 439
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- angular实时显示checkbox被选中的元素
/** * Created by zh on 20/05/15. */ // Code goes here var iApp = angular.module("App", []) ...
- Linux 如何实现 VLAN
LAN 表示 Local Area Network,本地局域网,通常使用 Hub 和 Switch 来连接 LAN 中的计算机.一般来说,两台计算机连入同一个 Hub 或者 Switch 时,它们就在 ...
- linux 抓取访问量排行
需求: 分析图片服务日志,把日志(每个图片访问次数*图片大小的总和)排行,取top10,也就是计算每个url的总访问大小 语句: awk '{a[$1]+=$10;}END{for(i in a){p ...
- lnux 下 core文件
1. core文件的简单介绍在一个程序崩溃时,它一般会在指定目录下生成一个core文件.core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的. 2. 开启或关闭core文件的生成用以下 ...
- bzoj 2694: Lcm
2694: Lcm Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 422 Solved: 220[Submit][Status][Discuss] ...
- matlab安装及破解
Matlab安装及破解: 笔者最近要做一些和建模相关的事,故此需要安装Matlab2017版.在此做下笔记. 网盘链接: 链接:https://pan.baidu.com/s/1lN8C7TDFjSV ...
- 深入理解iPhone数据持久化(手把手教你iphone开发 – 基础篇)
在所有的移动开发平台数据持久化都是很重要的部分:在j2me中是rms或保存在应用程序的目录中,在symbian中可以保存在相应的磁盘目录中和数据库中.symbian中因为权限认证的原因,在3rd上大多 ...
- ios下读取jason中的nsstring时间并本地化成中文gmt时间显示上午下午
https://developer.apple.com/library/ios/qa/qa1480/_index.html - (NSDate *)dateFromString:(NSString * ...
- 伙伴算法与slab算法
伙伴算法: 1.将空闲页面分为m个组,第1组存储2^0个单位的内存块,,第2组存储2^1个单位的内存块,第3组存储2^2个单位的内存块,第4组存储2^3个单位的内存块,以此类推.直到m组. 2.每个组 ...
- BUPT复试专题—最小距离查询(2013)
题目描述 给定一个由小写字母a到z组成的字符串S,其中第i个字符为S[i](下标从0开始).你需要完成下面两个操作:INSERT c 其中c是一个待输入的字符.你需要在字符串的末尾添加这个字符.保证 ...