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


  • 相关阅读:
    C# 实现将 PDF 转文本的功能
    推荐大家使用的CSS书写规范、顺序
    Android 应用开发者必看的 9 个 Tips
    网站统计中的数据收集原理及实现
    JSON劫持漏洞攻防原理及演练
    巧妙使用 CSS3 的褪色和动画效果制作消息提醒框
    20条常见的编码陷阱
    11 个最佳 jQuery 滚动条插件
    JavaScript客户端MVC 框架综述
    20个初学者实用的CSS技巧
  • 原文地址:https://www.cnblogs.com/liyexin/p/10951784.html
Copyright © 2011-2022 走看看