博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
骑士的移动
阅读量:5032 次
发布时间:2019-06-12

本文共 4102 字,大约阅读时间需要 13 分钟。

 

题目大意:

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.

 

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.

 

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

 

 

e2 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6

 

 

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.

 

 思路:
        用BFS广度优先搜索法,来寻找最短的路程。用队列储存找到的结点,先把骑士移动的八个方向表示出来,整个棋盘都初始化0,然后每个方向都搜索一遍,走过的地方设置为1,像撒网一样,把找到的结点放进队列中,再以这个结点为中心,八个方向继续撒网,能走通的地方就继续走,不能走就换方向。
 
源代码:
 
 
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 struct tnode{ 7 int x; 8 int y; 9 int step;10 11 }p,new_dot,t;12 int chess[10][10];13 string a1, a2;14 int dest_x,dest_y;15 int to[8][2] = { -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, -2, -2, -1 }; //骑士初始的八个方向16 int judge(int a, int b) //判断骑士是否还在棋盘内17 {18 if (a >=8 ||a<0||b >=8 ||b<0||chess[a][b]) 19 return 1;20 return 0;21 }22 int bfs()23 {24 25 queue
T; 26 memset(chess, 0, sizeof(chess));27 p.x = a1[0] - 'a'; // 骑士开始的位置28 p.y = a1[1] - '1'; 29 p.step = 0;30 chess[p.x][p.y] = 1; //把开始的位置设为131 dest_x = a2[0] - 'a'; //骑士要去的位置32 dest_y = a2[1] - '1';33 T.push(p); //把结点放到队列中34 while (!T.empty())35 { 36 t = T.front();37 T.pop();38 if (t.x == dest_x&&t.y == dest_y) //到达目标位置39 return t.step;40 41 for (int i = 0; i < 8; i++)42 {43 new_dot.x = t.x + to[i][0]; //8个方向,每个方向都遍历一遍44 new_dot.y = t.y + to[i][1]; 45 if (new_dot.x == dest_x&&new_dot.y == dest_y)46 return t.step + 1;47 if (judge(new_dot.x, new_dot.y)) //不是目标点,但是还在棋盘内,继续走48 continue;49 chess[new_dot.x][new_dot.y] = 1;50 new_dot.step = t.step + 1;51 T.push(new_dot); //把新结点放进队列中52 53 }54 }55 56 return 0;57 }58 int main()59 {60 while (cin >> a1 >> a2)61 {62 cout << "To get from " << a1 << " to " << a2 << " takes " << bfs() << " knight moves." << endl;63 }64 system("pause");65 return 0;66 67 }

 

 

心得:

    以前不了解BFS,一种用来找最短的距离,或者最少次数的算法,就像撒网一样,每次新学一点就会觉得很满足,暑假培训两天了,第一次体验这种生活,虽然辛苦,但是学到东西就觉得很值,就像屠老师说的,好好刷题,开始可以跟着别人的代码写,每个人都有一个过程,坚持总比不坚持收获得多!

 

 

 

转载于:https://www.cnblogs.com/Lynn0814/p/4675237.html

你可能感兴趣的文章
sed扩展命令使用
查看>>
关于异或
查看>>
抽象类,抽象方法
查看>>
山寨机与国产手机的历史
查看>>
FFMPEG转码后得到的MP4必须要加载完才能播放的问题
查看>>
刷过一题之NOIP2013表达式求值
查看>>
【HNOI2015】菜肴制作
查看>>
javascript继承
查看>>
海思uboot启动流程详细分析(三)【转】
查看>>
.NET之JSON序列化运用
查看>>
一般8位的微型机系统以16位来表示地址,则该计算机系统有几个地址空间
查看>>
sitemesh2.x+velocity+springmvc乱码解决方案
查看>>
(单例设计模式之一)饿汉式的反射与反序列化漏洞
查看>>
java中的final关键字和java抽象类
查看>>
用CSS3制作很特别的波浪形菜单
查看>>
Python面向对象03/继承
查看>>
Bing Maps进阶系列二:使用GeocodeService进行地理位置检索
查看>>
vector容器
查看>>
IDEA中使用mybatis逆向工程
查看>>
java序列化和反序列化
查看>>