Knight Moves
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13974   Accepted: 7797

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

 
1)37行是很重要的!好好想想为什么
2)输入的格式get!
 
代码:
 #include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring> using namespace std; char s[];
bool v[][];
int ex,ey,sx,sy,ans;
int dx[]={,,-,-,-,-,,};
int dy[]={-,-,-,-,,,,};//八个方向 struct node
{
int x,y,step;
}cur,nxt; queue<node>q; void bfs()
{
if(ex==sx&&ey==sy) //特判起点等于终点 ,找到后进行输出就一定是最小的
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",char(ex+'a'-),ey,char(sx+'a'-),sy,);
return;//格式
}
while(!q.empty()) q.pop(); // 多组数据初始化
memset(v,,sizeof(v)); // 同上
cur.x=ex,cur.y=ey; cur.step=; //起点
v[ex][ey]=true; //不要漏了标记起点
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop(); //不要漏了当前出队
//v[cur.x][cur.y]=false; 出队,清除标记,是否需要?不需要,为什么?
for(int i=;i<;i++) //八方位搜索
{
int xx=cur.x+dx[i],yy=cur.y+dy[i];
if(xx>&&xx<=&&yy>&&yy<=&&!v[xx][yy])
{
if(xx==sx&&yy==sy) //找到了,第一个找到的一定就是最近的,why?
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",char(ex+'a'-),ey,char(sx+'a'-),sy,cur.step+);
return ;
}
nxt.x=xx, nxt.y=yy; nxt.step=cur.step+;
v[nxt.x][nxt.y]=true;
q.push(nxt); //扩展出的状态入队
}
}
}
} int main()
{
while(scanf("%s",s)!=EOF) //注意输入,scanf读到空格
{
ex=s[]-'a'+; ey=s[]-'';
scanf("%s",s);
sx=s[]-'a'+; sy=s[]-'';
bfs();
}
}

POJ Knight Moves 2243 x的更多相关文章

  1. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  2. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  3. POJ 2243 Knight Moves

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13222   Accepted: 7418 Des ...

  4. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

  5. POJ 1915 Knight Moves

    POJ 1915 Knight Moves Knight Moves   Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29 ...

  6. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  7. HDU 2243 Knight Moves

    题目: A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find th ...

  8. POJ---2243 Knight Moves 使用A*算法的广度优先搜索

    题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...

  9. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

随机推荐

  1. Java多线程学习——死锁的一个容易理解的例子

    发生死锁的情况:多个线程需要同时占用多个共享资源而发生需要互相死循环等待的情况 public class Mirror { //镜子 } public class Lipstick { //口红 } ...

  2. bzoj3028食物 关于(1+x+x2+x3+x4+...)^k的第i项系数就是c(i+k−1,k−1)的证明

    关于(1+x+x2+x3+x4+...)^k的第i项系数就是c(i+k−1,k−1)的证明对于第i项,假设为5x^5=x^0*x^5x^5=x^1*x^4x^5=x^2*x^3........也就是说 ...

  3. centos6.9 安装mysql8

    centos6.9 安装 mysql8 # 安装mysql8 1.下载https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.16-2.el6.x86 ...

  4. [19/05/29-星期三] JavaScript_ 函数的简介

    一.函数的简介 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  5. 替换url不刷新页面

    今天碰到一个有趣的问题, 从其他站点登录后,放回了一个token, 但是我切换了路由之后token还在, 路由直接跟在了token参数后面, 后面先利用location.href替换掉原来的连接, 但 ...

  6. 1、Java语言概述与开发环境——JDK JRE JVM理解

    一.理解概念: 1.JDK(Java Development Kit Java开发工具包) JDK是提供给Java开发人员使用的,其中包含Java的开发工具,也包括JRE,所以安装了JDK,就不用单独 ...

  7. You-Get,多网站视频下载工具,非常方便

    You-Get是一个非常优秀的网站视频下载工具.使用You-Get可以很轻松的下载到网络上的视频.图片及音乐. 按Win+R键打开运行,输入cmd,再输入命令 pip install you-get, ...

  8. 自己制作 Android Vector Asset 矢量图

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/c5138891/article/deta ...

  9. MySQL索引的分类、结构、使用场景

    MySQL索引分类 1.主键索引:设定为主键后数据库会自动建立索引,innodb为聚簇索引 语法: 随表一起建索引: CREATE TABLE customer (id INT(10) UNSIGNE ...

  10. mpg123 - 播放 MPEG 1.0/2.0 Layer-1, -2, -3 音频文件

    语法 mpg123 [ -tscvqy01m24 ][ -b size ][ -k num ][ -n num ][ -f factor ][ -r rate ][ -g gain ][ -a dev ...