A - Shortest path of the king (棋盘)

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.

Examples

Input
a8
h1
Output
7
RD
RD
RD
RD
RD
RD
RD

    cf此题地址http://codeforces.com/problemset/problem/3/A

    这里学到了string这个东西。比如,string a; a[0]="adasdasd",第一次知道还有这种操作,一位存一个字符串,那么就简单多了。
    那么使用while模拟就好了,一直接近终点。肯定尽量先斜着走。while根据两者的位置分成好几种情况。根据ASCLL码,直接++,--就好了,不用再变成数字了。string s。s[0],s[1]直接++--就好了,直到等于e[0],e[1]。
使用一个tot来计数,此为总步数。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
string map[500];
string s,e;
int main()
{
    int tot=0;
    cin>>s;
    cin>>e;
    while(s[0]<e[0]&&s[1]<e[1])
    {
        s[0]++;s[1]++;
        map[tot++]="RU";
    }
    while(s[0]<e[0]&&s[1]>e[1])
    {
        s[0]++;s[1]--;
        map[tot++]="RD";
    }
    while(s[0]>e[0]&&s[1]<e[1])
    {
        s[0]--;s[1]++;
        map[tot++]="LU";
    }
    while(s[0]>e[0]&&s[1]>e[1])
    {
        s[0]--;s[1]--;
        map[tot++]="LD";
    }
    while(s[0]==e[0]&&s[1]>e[1])
    {
        s[1]--;
        map[tot++]="D";
    }
    while(s[0]==e[0]&&s[1]<e[1])
    {
        s[1]++;
        map[tot++]="U";    
    } 
    while(s[1]==e[1]&&s[0]<e[0])
    {
        s[0]++;
        map[tot++]="R";
    }
    while(s[1]==e[1]&&s[0]>e[0])
    {
        s[0]--;
        map[tot++]="L";
    }
    cout<<tot<<endl;
    for(int i=0;i<tot;i++)
        cout<<map[i]<<endl;
}


猜你喜欢

转载自www.cnblogs.com/liyexin/p/10951784.html