• 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
    RD
    RD
    RD
    RD
    RD
    RD
    RD
    题目大意:在棋盘上要从a(a8)到(h1)。输出最小的步数和路线(其中L:向左,R:向右,U:向上,D:向下)
    解题思路:这题其实很简单.....横向长度和纵向长度都是确定的,主要刚开始看这道题的时候,看到国王可以斜着走于是想复杂,其实不管
    国王是左上方斜走,右上斜走,左下斜走,右下斜走,有两点是唯一不变的,
    (1)斜着走必定会在x方向改变1,y方向改变1。
    (2)四个斜着走的方向在每次测试用例中只会出现一种。
    (就是比如终点在右下方,国王在斜着走的步骤就只会选择右下了,如果不是唯一,则肯定不是最短路径!)
    然后下面的代码就很容易理解了.
    AC代码1:比较简单易懂
    #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{
            y=-y;
            d='U';
        }
        printf("%d",x>y?x:y);
        while(x||y){
            printf("
    ");
            if(x){
                x--;
                printf("%c",c);        
            }
            if(y){
                y--;
                printf("%c",d);
            }
        }
        return 0;
    } 
    View Code

       AC代码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("
    "); 
            if(x){
                x--;
                printf("%c",c);
            }
            if(y){
                y--;
                printf("%c",d); 
            }
        }
        return 0;
    }
    View Code



  • 相关阅读:
    c# 把对象加入队列,对象为全局变量,对象改变队列值也跟着改变
    C# 一个数组未赋值引发的错误
    c# 2016QQ自动登录程序
    当时钟事件声明为过程变量 让system.threading.timer时钟失效
    if 循环的深入理解 哈希表的一种应用
    VB6对象与地址相互转换
    VB6的函数指针传递
    .net framework 4.0 从 GAC 卸载 程序集
    .net framework 4.0 从 GAC 卸载 程序集
    GAC in CLR 3.0
  • 原文地址:https://www.cnblogs.com/lipu123/p/12150824.html
Copyright © 2020-2023  润新知