ZOJ 1091 Knight Moves(BFS)
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)的更多相关文章
- 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 ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- 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 ( ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- poj1915 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...
- uva439 - Knight Moves(BFS求最短路)
题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...
- Knight Moves(BFS,走’日‘字)
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
- 006 01 Android 零基础入门 01 Java基础语法 01 Java初识 06 使用Eclipse开发Java程序
006 01 Android 零基础入门 01 Java基础语法 01 Java初识 06 使用Eclipse开发Java程序 Eclipse下创建程序 创建程序分为以下几个步骤: 1.首先是创建一个 ...
- Camera光学、成像和 3A 算法 (视觉),camera开发
简单介绍 转载:https://blog.csdn.net/ShareUs/article/details/94295628 成像与光学.计算机视觉,图像处理,数字成像.自动驾驶与视觉. 镜头设计:人 ...
- Arduino 串口的一些高级用法
来源: 1.配置串口通信数据位.校验位.停止位 通常我们使用Serial.begin(speed)来完成串口的初始化,这种方式,只能配置串口的波特率. 而使用Serial.begin(speed, c ...
- 【题解】SP1811 LCS - Longest Common Substring
\(\color{purple}{Link}\) \(\text{Solution:}\) 题目要求找到两个串的最长公共子串.\(LCP\) 我们将两个串中间和末尾插入终止符,并弄到一棵后缀树上去. ...
- Create a cursor from hardcoded array instead of DB
https://stackoverflow.com/questions/18290864/create-a-cursor-from-hardcoded-array-instead-of-db Crea ...
- 简单的Linux下的socket通信,小程序,方便以后查看。
首先是我的一个出错提示的头文件<myerr.h>,自从用了根本停不下来啊!!! #ifndef _MYERR_H_ #define _MYERR_H_ #include <stdio ...
- Linux系统编程 —线程同步概念
同步概念 同步,指对在一个系统中所发生的事件之间进行协调,在时间上出现一致性与统一化的现象. 但是,对于不同行业,对于同步的理解略有不同.比如:设备同步,是指在两个设备之间规定一个共同的时间参考:数据 ...
- 编程福利:50本C语言电子书,你还怕没书看吗!
推荐适合编程新手入门的几本经典的C语言书籍. 1.<C程序设计语言> C语言之父的著作,被称为C语言的的圣经.全球最经典的C语言教程.这本书最大的特点是精炼.读起来不会觉得"啰嗦 ...
- docker-compose编写示例
docker-compose.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...