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

  • 相关阅读:
    面向对象进阶
    初识面向对象
    模块和包
    day 17递归函数
    pip命令无法使用
    Python中的iteritems()和items()
    C# 截取字符串
    Python连接Mysql数据库
    【PYTHON】 Missing parentheses in call to 'print'
    javaScript中with函数用法实例分析
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4841590.html
Copyright © 2011-2022 走看看