CUC-SUMMER-CONTEST-1-A

A - Shortest path of the king
CodeForces - 3A

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.


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).

Input
The first line contains the chessboard coordinates of square s, the second line — of square t.
Chessboard coordinates consist of two characters, the first one is a lowercase Latin letter (from a to h), the second one is a digit from 1 to 8.

Output
In the first line print n — minimum number of the king's moves. Then in n lines print the moves themselves. Each move is described with one of the 8: L, R, U, D, LU, LD, RU or RD.
L, R, U, D stand respectively for moves left, right, up and down (according to the picture), and 2-letter combinations stand for diagonal moves. If the answer is not unique, print any of them.

Example
Input
a8
h1

Output
7
RD
RD
RD
RD
RD
RD
RD


题意:给定起始和终点坐标,问从起始最少经过几次移动能到达终点,移动方式有八种,上下左右,左上左下,右上右下。

解法:先斜向移动,使当前横坐标或纵坐标等于终点坐标时再水平或垂直移动。

代码:

#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
    char x1,y1,x2,y2;
    cin>>x1>>y1>>x2>>y2;
    cout<<max(abs(x1-x2),abs(y1-y2))<<endl;
    while(1){
        if(x1==x2&&y1==y2)
            break;
        if(x2>x1){
            x1++;
            cout<<"R";
        }
        else if(x2<x1){
            x1--;
            cout<<"L";
        }
        if(y2>y1){
            y1++;
            cout<<"U";
        }
        else if(y1>y2){
            y1--;
            cout<<"D";
        }
        cout<<endl;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,784评论 0 23
  • 简介 Glide是android快速有效开源的媒体管理和图片加载框架,这个框架包括媒体编码,内存和硬盘缓存,资源池...
    梵依然阅读 2,350评论 0 5
  • 天地亘古一棋局, 一将一帅挥众卒。 无中生有有化无, 生生死死迷归途。
    狄克先生阅读 291评论 0 3
  • #口号(王默涵的时间管理打卡)# 孩子第一个30天目标:每天放学回家吃青蛙 妈妈第一个30天目标:陪伴儿子成长,每...
    爱当娜阅读 244评论 0 0
  • 针对Android 的图片加载,有着太多的细节问题,需要注意,本文针对 Universal Image Loade...
    alighters阅读 1,115评论 0 6

友情链接更多精彩内容