zoukankan      html  css  js  c++  java
  • CF 3A-Shortest path of the king

    CF 3A-Shortest path of the king   点击打开链接

    题意:在国际象棋棋盘上求国王到皇后的距离,并打印走法,国王可以向周围8个方向移动

    思路:先将国王和皇后的坐标装换为直角坐标,如字母 B 对应   B-'A'=1.然后分别求出横坐标,纵坐标的差,两者的绝对值就是要走的步数,不理解的可以自己再纸上画一下。打印路径的话 ,则看差值的正负即可。 

    #include <iostream>
    #include<cmath>
    using namespace std;
    
    int main()
    {
        char sx,ex;
        int sy,ey;
        int a,b;
        cin>>sx>>sy;
        cin>>ex>>ey;
        a=sx-ex;
        b=sy-ey;
        int o=abs(a);
        int p=abs(b);
        int ans= o < p ? o : p;
        ans+=abs(o-p);
        cout<<ans<<endl;
            while(a>0&&b>0){
                a-=1;
                b-=1;
                cout<<"LD"<<endl;
            }
            while(a<0&&b<0){
                a+=1;
                b+=1;
                cout<<"RU"<<endl;
            }
            while(a>0&&b<0){
                a-=1;
                b+=1;
                cout<<"LU"<<endl;
            }
            while(a<0&&b>0){
                a+=1;
                b-=1;
                cout<<"RD"<<endl;
            }
            if(a==0&&b!=0){
                while(b>0){
                    b-=1;
                    cout<<"D"<<endl;
                }
                while(b<0){
                    b+=1;
                    cout<<"U"<<endl;
                }
            }
            else if(a!=0&&b==0){
                while(a>0){
                    a-=1;
                    cout<<"L"<<endl;
                }
    
                while(a<0){
                    a+=1;
                    cout<<"R"<<endl;
                }
            }
        return 0;
    }
    

  • 相关阅读:
    BZOJ1477 青蛙的约会
    Code Style
    线段树合并
    动态开点
    主席树
    启发式合并
    树的重心
    树的直径
    扩展欧几里得
    裴蜀定理
  • 原文地址:https://www.cnblogs.com/Levi-0514/p/9042500.html
Copyright © 2011-2022 走看看