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;}