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;
    }
    

  • 相关阅读:
    hdu 2647 Reward
    hdu 2094 产生冠军
    hdu 3342 Legal or Not
    hdu 1285 确定比赛名次
    hdu 3006 The Number of set
    hdu 1429 胜利大逃亡(续)
    UVA 146 ID Codes
    UVA 131 The Psychic Poker Player
    洛谷 P2491消防 解题报告
    洛谷 P2587 [ZJOI2008]泡泡堂 解题报告
  • 原文地址:https://www.cnblogs.com/Levi-0514/p/9042500.html
Copyright © 2011-2022 走看看