• A


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


  • 相关阅读:
    #Python#中国大学生实习-大数据分析
    PHP 数字转大写
    pdo封装2
    python tkinter组件学习
    微信小程序相关资料整理
    php任务管理器 —— Jobby
    linux 定时任务未执行php脚本
    centos中安装php7
    php资源列表
    给二维码(图片)添加文字(水印),让生成的二维码中间带logo
  • 原文地址:https://www.cnblogs.com/liyexin/p/10951784.html
Copyright © 2020-2023  润新知