zoukankan      html  css  js  c++  java
  • Codeforces 3A

    A. Shortest path of the king

    time limit per test
    1 second
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    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 1to 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

    题意:给出两个king棋子在国际象棋棋盘中的位置(棋盘见题目描述中的图),king每次可以从八个方向中选取一个走一步,问第一个king至少走几步能走到第二个king的位置。输出最少的步数和走的方向。

    题解:模拟。其实这题本可以用BFS做,但是懒得打BFS了,直接模拟出了答案。。。

       考虑一下,其实最少走的步数就是两个棋子横纵坐标之差的较大值,然后走的方向与两个棋子本身的相对方向有关,具体讨论参见代码,思路是非常显而易见的。。。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int x,y,p,q,Min,Max;
     4 char s[5];
     5 int main()
     6 {
     7     scanf("%s",s);
     8     x=s[0]-'a'+1;
     9     y=s[1]-'0';
    10     scanf("%s",s);
    11     p=s[0]-'a'+1;
    12     q=s[1]-'0';
    13     Min=min(abs(x-p),abs(y-q));
    14     Max=max(abs(x-p),abs(y-q));
    15     printf("%d
    ",Max);
    16     if (Min==abs(x-p))
    17     {
    18         if (x<=p&&y<=q)
    19         {
    20             for (int i=1;i<=Min;++i)
    21             printf("RU
    ");
    22             for (int i=Min+1;i<=Max;++i)
    23             printf("U
    ");
    24         }
    25         if (x<=p&&y>q)
    26         {
    27             for (int i=1;i<=Min;++i)
    28             printf("RD
    ");
    29             for (int i=Min+1;i<=Max;++i)
    30             printf("D
    ");
    31         }
    32         if (x>p&&y<=q)
    33         {
    34             for (int i=1;i<=Min;++i)
    35             printf("LU
    ");
    36             for (int i=Min+1;i<=Max;++i)
    37             printf("U
    ");
    38         }
    39         if (x>p&&y>q)
    40         {
    41             for (int i=1;i<=Min;++i)
    42             printf("LD
    ");
    43             for (int i=Min+1;i<=Max;++i)
    44             printf("D
    ");
    45         }
    46     }
    47     else if (Min==abs(y-q))
    48     {
    49         if (x<=p&&y<=q)
    50         {
    51             for (int i=1;i<=Min;++i)
    52             printf("RU
    ");
    53             for (int i=Min+1;i<=Max;++i)
    54             printf("R
    ");
    55         }
    56         if (x<=p&&y>q)
    57         {
    58             for (int i=1;i<=Min;++i)
    59             printf("RD
    ");
    60             for (int i=Min+1;i<=Max;++i)
    61             printf("R
    ");
    62         }
    63         if (x>p&&y<=q)
    64         {
    65             for (int i=1;i<=Min;++i)
    66             printf("LU
    ");
    67             for (int i=Min+1;i<=Max;++i)
    68             printf("L
    ");
    69         }
    70         if (x>p&&y>q)
    71         {
    72             for (int i=1;i<=Min;++i)
    73             printf("LD
    ");
    74             for (int i=Min+1;i<=Max;++i)
    75             printf("L
    ");
    76         } 
    77     }
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    webpack-dev-server报错
    npm run dev 报错 run `npm audit fix` to fix them, or `npm audit` for details
    vscode里使用.vue代码模板的方法
    console.log()中的%d,%s等代表的输出类型
    使用Babel将单独的js文件 中的 ES6转码为ES5
    jQuery选择什么版本 1.x? 2.x? 3.x?
    在线jquery.min.js、vue.min.js引用
    WebSocket对象的“readyState”属性记录连接过程中的状态值
    vue项目中,使用vue-awesome-swiper插件实现轮播图
    vue-cli 项目里屏幕自适应
  • 原文地址:https://www.cnblogs.com/zk1431043937/p/7587987.html
Copyright © 2011-2022 走看看