zoukankan      html  css  js  c++  java
  • 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



  • 相关阅读:
    【Java】推断文件的后缀名
    UVa 131
    Java开发手冊 Java学习手冊教程(MtJava开发手冊)
    《Java并发编程实战》第十五章 原子变量与非堵塞同步机制 读书笔记
    OC语言--NSFileManager&amp; NSFileHandle
    为什么我刚发表的文章变成了“待审核”,csdn有没有官方解释啊
    mac os升级为 Yosemite 10.10 后不能创建javaproject
    【spring】在spring cloud项目中使用@ControllerAdvice做自定义异常拦截,无效 解决原因
    【mybatis】mybatis动态order by 的问题, 注意 只需要把#{} 改成 ${} 即可
    【spring cloud】一个ms微服务想要给注册中心eureka发现,需要满足这些条件,微服务不能被eureka注册中心发现的解决方案
  • 原文地址:https://www.cnblogs.com/lipu123/p/12150824.html
Copyright © 2011-2022 走看看