codeforces3A

Shortest path of the king

 CodeForces - 3A 

棋盘上的国王被单独放置。尽管他是孤独的,但并未伤心,因为他有事关全局的重要性。例如,他必须正式访问方格 t 。由于国王不习惯于浪费自己的时间,因此他想用最小的移动步数,从自己的当前位置 s 抵达方格 t 。请帮助他达成这一目标。

每次移动,国王可以从当前的方格开始,抵达与之具有公共边或公共顶点的某个方格 (通常他可以走向 8 个不同的方格)。

输入

第一行包含了方格 s 所在的棋盘坐标,第二行 — 方格 t 。

棋盘坐标包含两个字符,第一个字符是小写的拉丁字母 (从 a 到 h),第二个字符是介于 1 到 8 之间的数字。

输出

在第一行中,打印 n — 国王移动的最少步数。然后,在 n 行中打印相应的移动步骤。每次移动的描述,使用以下 8 种方式中的一种:L, R, U, D, LU, LD, RU 或 RD 。

L, R, U, D 相应表示左移、右移、上移和下移 (依据图片),且两字母组合表示斜对角移动。如果答案不唯一,打印它们中的任何一种。

示例

输入
a8
h1
输出
7
RD
RD
RD
RD
RD
RD
RD

sol:暴力if判断8个方向即可
Ps:代码非常丑丑丑丑丑
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0');    return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('
')
int main()
{
    int x1,y1,x2,y2;
    char ch=' ';
    while(!islower(ch)) ch=getchar();
    x1=ch-'a'+1;
    R(y1);
    ch=' ';
    while(!islower(ch)) ch=getchar();
    x2=ch-'a'+1;
    R(y2);
    Wl(max(abs(x1-x2),abs(y1-y2)));
    if(x1<=x2)
    {
        if(y1<=y2)
        {
            if(x2-x1>y2-y1)
            {
                while(y2-y1>0) {puts("RU"); x1++; y1++;}
                while(x2-x1>0) {puts("R"); x1++;}
            }
            else
            {
                while(x2-x1>0) {puts("RU"); x1++; y1++;}
                while(y2-y1>0) {puts("U"); y1++;}
            }
        }
        else
        {
            if(x2-x1>y1-y2)
            {
                while(y1-y2>0) {puts("RD"); x1++; y1--;}
                while(x2-x1>0) {puts("R"); x1++;}
            }
            else
            {
                while(x2-x1>0) {puts("RD"); x1++; y1--;}
                while(y1-y2>0) {puts("D"); y1--;}
            }
        }
    }
    else
    {
        if(y1<=y2)
        {
            if(x1-x2>y2-y1)
            {
                while(y2-y1>0) {puts("LU"); x1--; y1++;}
                while(x1-x2>0) {puts("L"); x1--;}
            }
            else
            {
                while(x1-x2>0) {puts("LU"); x1--; y1++;}
                while(y2-y1>0) {puts("U"); y1++;}
            }
        }
        else
        {
            if(x1-x2>y1-y2)
            {
                while(y1-y2>0) {puts("LD"); x1--; y1--;}
                while(x1-x2>0) {puts("L"); x1--;}
            }
            else
            {
                while(x1-x2>0) {puts("LD"); x1--; y1--;}
                while(y1-y2>0) {puts("D"); y1--;}
            }
        }
    }
    return 0;
}
View Code
 
原文地址:https://www.cnblogs.com/gaojunonly1/p/10580491.html