Shortest Path of the King CodeForces #3A 题解[C++]

该博客详细解析了CodeForces竞赛中的#3A问题,国王如何从棋盘上的某个位置s到达目标位置t的最短路径。通过贪心算法,每次选择距离目标最近的一步进行移动,确保找到最短路径。提供了具体的解题思路及代码实现。
摘要由CSDN通过智能技术生成

题目来源


http://codeforces.com/contest/3/problem/A

题意解析


The king is left alone on the chessboard. In spite of this loneliness, he doesn’t lose heart, because he has business of national importance. For example, he has to pay an official visit to square t. As the king is not in habit of wasting his time, he wants to get from his current position s to square t in the least number of moves. Help him to do this.
chessboard

In one move the king can get to the square that has a common side or a common vertex with the square the king is currently in (generally there are 8 different squares he can move to).

Examples
input

a8 h1

output


7
RD
RD
RD
RD
RD
RD
RD

给出一个上图所示的棋盘, 棋盘上现有孤王一只. 输入它目前的位置 s, 和目的地 t, 求出s->t的最短路长度和最短路各节点. 棋盘上的坐标用 “a1”, “h8”这样的点来表示.

解题思路


水题. 直接用贪心算法求解即可. 容易看到, 在王能走的八个方向中, 也就是说在王下一步可能到达的八个点中, 只可能有一个点距离目的地有最短距离. 我们找出它来直接作为下一跳即可. 每一跳都选择最优选择, 那么容易看出, 最后得出的路径必然是最短路径.

代码实现



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值