码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces- Shortest path of the king

时间:2020-01-05 00:08:28      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:rac   ica   mamicode   fir   line   clip   思路   步骤   mon   

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

 




codeforces- Shortest path of the king

标签:rac   ica   mamicode   fir   line   clip   思路   步骤   mon   

原文地址:https://www.cnblogs.com/lipu123/p/12150824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!