POJ2243 Knight Moves —— A*算法

题目链接:http://poj.org/problem?id=2243


 

Knight Moves
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14500 Accepted: 8108

 

Description

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of 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

The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

Output

For each test case, print one line saying "To get from xx to yy takes n knight moves.".

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

Ulm Local 1996

 




题解:

这题直接用BFS就可以过了。但是想借助这题学一下A*算法(但以下写法好像不是完整的或者说正确的写法,先放一放,以后遇到再深入)。



代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <queue>
 6 #include <cmath>
 7 using namespace std;
 8 typedef long long LL;
 9 const int INF = 2e9;
10 const LL LNF = 9e18;
11 const int MOD = 1e9+7;
12 const int MAXN = 8+10;
13 
14 struct node
15 {
16     int x, y, step;
17     int g, h, f;
18     bool operator<(const node&a)const{
19         return f>a.f;
20     }
21 };
22 bool vis[MAXN][MAXN];
23 int dir[8][2] = { {1,2}, {1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1} };
24 
25 int gdis(int x_step, int y_step)    //直线距离
26 {
27     return (int)sqrt(x_step*x_step+y_step*y_step)*10+1;
28 }
29 
30 int hdis(node a, node b)    //曼哈顿距离
31 {
32     return ( abs(a.x-b.x) + abs(a.y-b.y) )*10;
33 }
34 
35 priority_queue<node>que;
36 int Astar(node st, node en)
37 {
38     memset(vis,false,sizeof(vis));
39     st.step = st.g = st.h = st.f = 0;
40     while(!que.empty()) que.pop();
41     que.push(st);
42     vis[st.x][st.y] = true;
43 
44     while(!que.empty())
45     {
46         node now = que.top();
47         que.pop();
48         if(now.x==en.x && now.y==en.y)
49             return now.step;
50 
51         for(int i = 0; i<8; i++)
52         {
53             node tmp;
54             tmp.x = now.x + dir[i][0];
55             tmp.y = now.y + dir[i][1];
56             if(tmp.x>=1 && tmp.x<=8 && tmp.y>=1 && tmp.y<=8 && !vis[tmp.x][tmp.y])
57             {
58                 tmp.step = now.step + 1;
59                 tmp.g = now.g + gdis(abs(dir[i][0]), abs(dir[i][1]));
60                 tmp.h = hdis(tmp, en);
61                 tmp.f = tmp.g + tmp.h;
62 
63                 vis[tmp.x][tmp.y] = true;
64                 que.push(tmp);
65             }
66         }
67     }
68 }
69 
70 int main()
71 {
72     char s1[10], s2[10];
73     while(scanf("%s%s", s1, s2)!=EOF)
74     {
75         node st, en;
76         st.x = s1[0]-'a'+1;
77         st.y = s1[1]-'0';
78         en.x = s2[0]-'a'+1;
79         en.y = s2[1]-'0';
80         int step = Astar(st,en);
81         printf("To get from %c%d to %c%d takes %d knight moves.\n",st.x+'a'-1,st.y,en.x+'a'-1,en.y,step);
82     }
83 }
View Code

 


 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7538602.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/268544.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c语言多重括号,大佬在吗,我用C写了一个去多重括号的函数,结果。。。

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include #include #include "malloc.h"char * changeOrder(char *s);struct Node{char *data;struct Node * link;};struct Node * top1 NULL;struct Node * top2 NULL;void Push1(char *s){struct Node * temp (str…

mac配置telnet

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" brew install telnet

linux下的DNS服务器详解

DNS&#xff1a;Domain Name System 域名管理系统 域名是由圆点分开一串单词或缩写组成的&#xff0c;每一个域名都对应一个惟一的IP地址&#xff0c;这一命名的方法或这样管理域名的系统叫做域名管理系统。 大家都知道&#xff0c;当我们在上网的时候&#xff0c;通常输入的是…

c语言一个循环重新输入密码,想程序高手求助--用C语言来编辑一个输入密码的程序...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼/*--------实现密码的隐式输入-----------------*/inputpw(char *password,int len) /*len为密码长度*/{int i0; /*密码数组索引值,同时也表示记录已显示*的数目*/char ch;fflush(stdin); /*清洗流&#xff0c;以防妨碍密码正确输入…

怎样玩转千万级别的数据

作者&#xff1a;Sam Xiaowww.cnblogs.com/xcj26/p/3305789.html如有好文章投稿&#xff0c;请点击 → 这里了解详情大数据处理是一个头疼的问题&#xff0c;特别当达不到专业DBA的技术水准时&#xff0c;对一些数据库方面的问题感到无奈。所以还是有必要了解一些数据库方面的技…

c语言多维数组基础知识,C语言基础第7章多维数组.ppt

C语言基础第7章多维数组7.2 二维数组及多维数组二维数组的定义定义方式&#xff1a;  数据类型 数组名[常量表达式][常量表达式]&#xff1b;;二维数组理解;二维数组元素的引用形式&#xff1a; 数组名[下标][下标]二维数组元素的初始化分行初始化&#xff1a;;程序举例;例 …

关于待机、休眠、睡眠的区别和优缺点

Windows中很早就加入了待机、休眠等模式&#xff0c;而Windows Vista中更是新加入了一种叫做睡眠的模式&#xff0c;可是很多人还是习惯在不使用电脑的时候 将其彻底关闭。其实充分利用这些模式&#xff0c;我们不仅可以节约电力消耗&#xff0c;还可以用尽可能短的时间把系统恢…

如何制作pem公钥证书和私钥证书

1.先下载openssl 下载地址 https://pan.baidu.com/s/1kUK1MJX 2.下载之后保存在某个盘里&#xff08;例如在F:/cer/openssl&#xff09; 3.打开windows的 cmd&#xff0c;切换到目录 F:/cer/openssl/openssl\bin 4.生成商户RSA私钥(执行如下命令) openssl genrsa -out rsa_…

苏州大学电子信息学院C语言,电子信息学院

专著Z. Zhu and A. K. Nandi, (2014) Automatic Modulation Classification - Principles, Algorithms, and Applications. Wiley (to be published in January 2015)期刊论文Z. Zhu, A. K.Nandi, (2014) Blind Digital Modulation Classification using Minimum Distance Cent…

网络协议集合2

这张是更加详细的网络协议图&#xff0c;希望会对学习有一定作用。学习网络知识&#xff0c;一定要打好框架&#xff0c;做到心中有“图”&#xff0c;通常我们只是接触TCP/IP协议&#xff0c;但对于其他体系和结构&#xff0c;也应当有所了解&#xff0c;这样能更好地理解网络…

C语言二月天数计算,关于计算两个日期间天数的代码,大家来看看

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼这是原贴:http://post.baidu.com/f?kz100411727这是原码:#include "stdio.h"main(){long int i,a[2],b[2],c[2],x[12]{0,31,59,90,120,151,181,212,243,273,304,334},y,z[2];scanf("%ld-%ld-%ld %ld-%ld-%ld"…

JAVA中ACTION层, SERVICE层 ,MODLE层 和 DAO层的功能区分

JAVA中ACTION层, SERVICE层 &#xff0c;MODLE层 和 DAO层的功能区分 首先这是现在最基本的分层方式&#xff0c;结合了SSH架构。modle层就是对应的数据库表的实体类。Dao层是使用了hibernate连接数据库、操作数据库&#xff08;增删改查&#xff09;。Service层&#xff1a;引…

自定义权限 android,如何在Android中使用自定义权限?

蛊毒传说我创建了一个测试代码&#xff0c;您可以使用它并测试您的权限。有两个应用程序PermissionTestClient声明权限并使用此权限保护其活动。这是清单文件&#xff1a;<?xml version"1.0" encoding"utf-8"?> …

.net c# 中获得GridView的EmptyDataTemplate中的控件的方法(转)

拖了一个GridView到页面&#xff0c;当没有数据时在EmptyDataTemplate中显示添加的TextBox&#xff0c;但EmptyDataTemplate中不像FooterTemplate可以直接TextBox BankCard GridView1.FooterRow.FindControl("BankCard") as TextBox;来获取。 研究了一翻&#xff0c;…

MYSQL在centos上主从配置

主从配置理论传送门:http://blog.csdn.net/hguisu/article/details/7325124 具体配置方案: 一&#xff1a;MYSQL主从配置 1.1 部署环境 主(master_mysql): 192.168.1.200 OS:CentOS 6.5 从(slave_mysql): 192.168.1.201 OS:CentOS 6.5 1.2 安装mysql 主和从: yu…