3A Codeforces - Shortest path of the king




Solution in c++

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

    string aa,bb;

    cin>>aa>>bb;

    int x1=aa[0]-'a',x2=bb[0]-'a',y1=aa[1]-'1',y2=bb[1]-'1';

    int cr=min(abs(x1-x2),abs(y1-y2));

    int all=abs(x1-x2)+abs(y1-y2)-cr;

    cout<<all<<"\n";

    while(all--){

        if(x1<x2){cout<<"R";x1++;}else if (x1>x2) {cout<<"L";x1--;}

        if(y1<y2){cout<<"U";y1++;}else if (y1>y2) {cout<<"D";y1--;}

        cout<<"\n";

    }

    return 0;

}

Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق