Knight Moves

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 Specification

The input file 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 Specification

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.
  1 //Problem Name: Knight Moves
2 //Source: ZOJ 1091
3 //Author: jinjin18
4 //Main idea:BFS
5 //Language: C++
6 //Point: init;deal with level in BFS--tail;input--gets rather than scanf;
7 //-upline input--sscanf;BFS model;
8 //======================================================================
9
10 #include<stdio.h>
11 #define MAXSIZE 100000
12 #define INF 10000000
13
14 typedef struct node{
15 char x;
16 int y;
17 }Point;
18 int visited[9][9];
19 void init(){ //初始化,需要二重循环,可用memset函数
20 for(int i = 0; i < 9 ;i++){
21 for(int j = 0; j <9; j++){
22 visited[i][j] = 0;
23 }
24 }
25 }
26 int BFS(char ax,int ay,char bx,int by){
27 int res = 0;
28 int tail = 1;
29 visited[ax-'a'+1][ay] = 1;
30 Point Q[MAXSIZE] = {0};
31 int pre = 0;
32 int last = 1;
33 Q[0].x = ax;
34 Q[0].y = ay;
35 while(pre < last){
36 //printf("OK");
37 char thisx = Q[pre].x;
38 int thisy = Q[pre].y;
39 pre++;
40 //printf("%d %c %d\n",res,thisx,thisy);
41 if(thisx == bx&&thisy == by){ //结束循环
42 return res;
43 }
44 if(thisx + 2 <= 'h'&&thisy + 1 <= 8&&visited[thisx+2-'a'+1][thisy+1]==0){
45 visited[thisx+2-'a'+1][thisy+1]=1;
46 Q[last].x = thisx+2;
47 Q[last].y = thisy+1;
48 last++;
49 }
50 if(thisx + 2 <= 'h'&&thisy - 1 >= 1&&visited[thisx+2-'a'+1][thisy-1]==0){
51 visited[thisx+2-'a'+1][thisy-1]=1;
52 Q[last].x = thisx+2;
53 Q[last].y = thisy-1;
54 last++;
55 }
56 if(thisx + 1 <= 'h'&&thisy + 2 <= 8&&visited[thisx+1-'a'+1][thisy+2]==0){
57 visited[thisx+1-'a'+1][thisy+2]=1;
58 Q[last].x = thisx+1;
59 Q[last].y = thisy+2;
60 last++;
61 }
62 if(thisx + 1 <= 'h'&&thisy - 2 >= 1&&visited[thisx+1-'a'+1][thisy-2]==0){
63 visited[thisx+1-'a'+1][thisy-2]=1;
64 Q[last].x = thisx+1;
65 Q[last].y = thisy-2;
66 last++;
67 }
68 if(thisx - 2 >= 'a'&&thisy - 1 >= 1&&visited[thisx-2-'a'+1][thisy-1]==0){
69 visited[thisx-2-'a'+1][thisy-1]=1;
70 Q[last].x = thisx-2;
71 Q[last].y = thisy-1;
72 last++;
73 }
74 if(thisx - 2 >= 'a'&&thisy + 1 <= 8&&visited[thisx-2-'a'+1][thisy+1]==0){
75 visited[thisx-2-'a'+1][thisy+1]=1;
76 Q[last].x = thisx-2;
77 Q[last].y = thisy+1;
78 last++;
79 }
80
81 if(thisx - 1 >= 'a'&&thisy - 2 >=1&&visited[thisx-1-'a'+1][thisy-2]==0){
82 visited[thisx-1-'a'+1][thisy-2]=1;
83 Q[last].x = thisx-1;
84 Q[last].y = thisy-2;
85 last++;
86 }
87 if(thisx - 1 >= 'a'&&thisy + 2 <= 8&&visited[thisx-1-'a'+1][thisy+2]==0){
88 visited[thisx-1-'a'+1][thisy+2]=1;
89 Q[last].x = thisx-1;
90 Q[last].y = thisy+2;
91 last++;
92 }
93 if(tail == pre){ //更新tail
94 tail = last;
95 res++;
96 }
97
98 }
99 return INF;
100
101 }
102
103 int main(){
104 int ay,by;
105 char ax,bx;
106 char S[10];
107 while(gets(S)){ //%s与%c不能用,思考为何?
108 sscanf(S,"%c%d %c%d",&ax,&ay,&bx,&by);
109 init();
110 int res = BFS(ax,ay,bx,by);
111 printf("To get from %c%d to %c%d takes %d knight moves.\n",ax,ay,bx,by,res);
112 }
113 return 0;
114
115 }

看到一篇写的比较好的博文:https://blog.csdn.net/z8110/article/details/49492479

ZOJ 1091 Knight Moves(BFS)的更多相关文章

  1. ZOJ 1091 (HDU 1372) Knight Moves(BFS)

    Knight Moves Time Limit: 2 Seconds      Memory Limit: 65536 KB A friend of you is doing research on ...

  2. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  3. poj2243 Knight Moves(BFS)

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

  4. HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏

    Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...

  5. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

  6. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  7. poj1915 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...

  8. uva439 - Knight Moves(BFS求最短路)

    题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...

  9. Knight Moves(BFS,走’日‘字)

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

随机推荐

  1. Java知识系统回顾整理01基础02面向对象03方法

    一.根据实例给出"方法"的定义 在LOL中,一个英雄可以做很多事情,比如超神,超鬼,坑队友 能做什么在类里面就叫做方法 比如队友残血正在逃跑,你过去把路给别人挡住了,导致他被杀掉. ...

  2. HTML & CSS & JavaScript 从一个表格到一个灰阶颜色表(目录)

    HTML & CSS & JavaScript 从一个表格到一个灰阶颜色表 01 HTML & CSS & JavaScript 从一个表格到一个灰阶颜色表 02 HT ...

  3. 【题解】[USACO07OPEN]Dining G

    \(Link\) \(\text{Solution:}\) 这一题,我们要做到,食物和牛.牛和饮料均为一对一的关系.我们发现这个图不好建立. 经典技巧:将牛拆边,拆成入点和出点,并连容量为\(1\)的 ...

  4. 证明RSA算法在明文和公私钥中N不互质情况下仍然成立

    关于RSA的基础过程介绍 下文中的 k 代表自然数常数,不同句子,公式中不一定代表同一个数 之前接触RSA,没有过多的思考证明过程,今天有感而发,推到了一遍 假设公钥 (e, N) , 私钥 (d, ...

  5. Android和CraigDJ

    下载HTML source - 46.8 KB 下载APK - 8.1 MB Introduction  大家好,欢迎来到我的Android应用程序项目! 我必须承认,我仍然对原生安卓环境几乎一无所知 ...

  6. Java防止文件被篡改之文件校验和

    Java防止文件被篡改之文件校验和转载:请注明出处,谢谢! 1.为什么要防止文件被篡改?  答案是显然的,为了保证版权,系统安全性等.之前公司开发一个系统,技术核心是一个科学院院士的研究成果,作为一款 ...

  7. 多测师_肖sir_git _004(版本控制器)

    gitgit 是一个开源的分布式版本控制系统,用于敏捷高效的处理任何大小的项目.git是linux torvalds 为了帮助管理linux内核开发的一个开放源码的版本控制软件.git与常用的版本控制 ...

  8. MeteoInfoLab脚本示例:中文处理

    在脚本中使用中文需要指明是unicode编码,即在含有中文的字符串前加u,比如:u'中文'.还需要将字体指定为一种中文字体.详见下面的例子.脚本程序: x = [1,2,3,4] y = [1,4,9 ...

  9. spring boot:用spring security加强druid的安全(druid 1.1.22 / spring boot 2.3.3)

    一,druid的安全保障有哪些环节要注意? 1,druid ui的访问要有ip地址限制 2,用户必须要有相应的权限才能访问druid 3,关闭重置功能 说明:stat-view-servlet.url ...

  10. CentOS下编译搭建LAMP环境

    搭建LAMP环境须知 搭建LAMP环境时,需要安装的所有软件都要按照一定的顺序安装,我们按照Apache->MySQL->PHP的顺序安装.但是在安装PHP之前,应先安装PHP5需要的最新 ...