codeforces- 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
Copy
a8 
h1
Output
Copy
. 7 
the RD
the RD
the RD
the RD
the RD
the RD
the RD
Title effect: on the board from a (a8) to (h1). The output of the minimum number of steps and route (where L: left, R: right, U: Up, D: down)
Outline of Solution: This problem is actually very simple ..... lateral length and longitudinal length are determined mainly just started to see this question when I saw the King can go sideways so want complicated, in fact, whether
the king is inclined to go top left, top right move diagonally, lower left oblique take the lower right move diagonally, two points are the only constant,
(1) will definitely go sideways 1 change in the x-direction, y direction change 1.
Direction (2) four sideways away monolingual appear in every test case.
(! Is such the end in the bottom right of the king in step sideways to go, we'll choose the lower right, if not unique, certainly not the shortest path)
then the following code is easy to understand.
AC Code 1: simple understandability
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e5+10;
char a[3],b[3];
int main()
{
    scanf("%s%s",a,b);
    char c,d;
    int x=a[0]-b[0],y=a[1]-b[1];
    if(x>0){
        c='L';
    }
    else{
        x=-x;
        c='R';
    }
    if(y>0){
        d='D';
    }
    else{
        and = - y;
        d='U';
    }
    printf("%d",x>y?x:y);
    while(x||y){
        printf("\n");
        if(x){
            x--;
            printf("%c",c);        
        }
        if(y){
            and - ;
            printf("%c",d);
        }
    }
    return 0;
} 
View Code

   AC Code 2:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e5+10;
char a[3],b[3];
int main()
{
    char c,d;
    scanf("%s%s",a,b);
    int x=a[0]-b[0],y=a[1]-b[1];
    c=((x<0)?x=-x,'R':'L');
    d=((y<0)?y=-y,'U':'D');
    printf("%d",x>y?x:y);
    while(x||y){
    printf("\n"); 
        if(x){
            x--;
            printf("%c",c);
        }
        if(y){
            and - ;
            printf("%c",d); 
        }
    }
    return 0;
}
View Code

 




Guess you like

Origin www.cnblogs.com/lipu123/p/12150824.html
Recommended