3A. Shortest path of the king

给你一个的棋盘,

问:从一个坐标到达另一个坐标需要多少步?
每次移动可以是八个方向。
 
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;
typedef long long LL;
const LL INF = 0xffffff;
const int maxn = 1005;
const LL MOD = 1e9+7;

void solve(int sx,int sy,int ex,int ey)
{
    int x = ex - sx;
    int y = ey - sy;
    int n = min(abs(x), abs(y));
    int step =  max(abs(x), abs(y)) ;
    printf("%d\n", step);
    for(int i=1; i<=n; i++)
    {
        if(x > 0 && y > 0) puts("RU");
        if(x > 0 && y < 0) puts("RD");
        if(x < 0 && y > 0) puts("LU");
        if(x < 0 && y < 0) puts("LD");
    }
    n = max(abs(x), abs(y)) - n;

    for(int i=1; i<=n; i++)
    {
        if( abs(x) > abs(y) && x > 0) puts("R");
        if( abs(x) > abs(y) && x < 0) puts("L");
        if( abs(y) > abs(x) && y > 0) puts("U");
        if( abs(y) > abs(x) && y < 0) puts("D");
    }
}

int main()
{
    char str[5];
    int sx, sy, ex, ey;
    scanf("%s", str);
    sx = str[0] - 'a' + 1;
    sy = str[1] - '0';
    scanf("%s", str);
    ex = str[0] - 'a' + 1;
    ey = str[1] - '0';
    solve(sx, sy, ex, ey);

    return 0;
}

 

posted @ 2015-09-26 23:27  向前走丶不回首  阅读(151)  评论(0)    收藏  举报