A. Shortest Path of the king

Source: Internet
Author: User
Time limit per test

1 second

Memory limit per test

64 megabytes

Input

Standard Input

Output

Standard output

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 squareT.
As the King is not in habit of wasting his time, he wants to get from his current positionSTo squareTIn
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 ).

Input

The first line contains the chessboard coordinates of SquareS, The second line-of SquareT.

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 1to 8.

Output

In the first line printN-Minimum number of the King's moves. Then inNLines
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.

Sample test (s) Input
a8h1
Output
7RDRDRDRDRDRDRD

Explanation: this is the shortest distance from one position on the board to another. You can move the distance from top to bottom to the diagonal line in eight directions. First, we should convert the coordinate coordinates of the Board letters into numbers to determine the differences between the horizontal and vertical coordinates. Because one step moves the coordinates at the same time, the moving distance must be at least the maximum of the two coordinates. When moving, equal coordinates are used as the termination condition. Otherwise, the direction is determined by constantly comparing the horizontal and vertical coordinates.

#include<iostream>#include<map>#include<string>#include<algorithm>using namespace std;int main(){string s1, s2;cin >> s1 >> s2;cout << max(abs(int(s1[0]-s2[0])), abs(int(s1[1]-s2[1]))) << endl;while (s1 != s2) {if (s1[0] < s2[0]) {s1[0]++;cout << "R";}else if (s1[0] > s2[0]){s1[0]--;cout << "L";}if (s1[1] > s2[1]) {s1[1]--;cout << "D"; }else if (s1[1] < s2[1]) {s1[1]++;cout << "U";}cout << endl;}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.