Advertisement
bappi2097

A. Shortest path of the king

Sep 9th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string str1[64];
  4. int main()
  5. {
  6.     ios_base::sync_with_stdio(false);
  7.     cin.tie(NULL);
  8.     #ifndef ONLINE_JUDGE
  9.     freopen("input.txt","r",stdin);
  10.     freopen("output.txt","w",stdout);
  11.     #endif // ONLINE_JUDGE
  12.     string str2,str3;
  13.     cin>>str2>>str3;
  14.     int r1,r2,c1,c2,cnt=0;
  15.     c1=str2[0]-'a'+1;
  16.     r1=str2[1]-'0';
  17.     c2=str3[0]-'a'+1;
  18.     r2=str3[1]-'0';
  19.     if(r1==r2)
  20.     {
  21.         if(c1-c2>0)for(int i=0;i<abs(c1-c2);i++,cnt++)str1[cnt]="L";
  22.         else if(c1-c2<0)for(int i=0;i<abs(c1-c2);i++,cnt++)str1[cnt]="R";
  23.     }
  24.     else if(c1==c2)
  25.     {
  26.         if(r1-r2>0)for(int i=0;i<abs(r1-r2);i++,cnt++)str1[cnt]="D";
  27.         if(r1-r2<0)for(int i=0;i<abs(r1-r2);i++,cnt++)str1[cnt]="U";
  28.     }
  29.     else
  30.     {
  31.         if(r2-r1>0 && c2-c1>0)
  32.         {
  33.             for(int i=r1,j=c1;i<=r2 && j<=c2;i++,j++)
  34.             {
  35.                 if(i==r2)
  36.                 {
  37.                     for(j;j<c2;j++)
  38.                     {
  39.                         str1[cnt]="R";
  40.                         cnt++;
  41.                     }
  42.                 }
  43.                 else if(j==c2)
  44.                 {
  45.                     for(i;i<r2;i++)
  46.                     {
  47.                         str1[cnt]="U";
  48.                         cnt++;
  49.                     }
  50.                 }
  51.                 else
  52.                 {
  53.                     str1[cnt]="RU";
  54.                     cnt++;
  55.                 }
  56.             }
  57.         }
  58.         else if(r2-r1>0 && c2-c1<0)
  59.         {
  60.             for(int i=r1,j=c1;i<=r2 && j>=c2;i++,j--)
  61.             {
  62.                 if(i==r2)
  63.                 {
  64.                     for(j;j>c2;j--)
  65.                     {
  66.                         str1[cnt]="L";
  67.                         cnt++;
  68.                     }
  69.                 }
  70.                 else if(j==c2)
  71.                 {
  72.                     for(i;i<r2;i++)
  73.                     {
  74.                         str1[cnt]="U";
  75.                         cnt++;
  76.                     }
  77.                 }
  78.                 else
  79.                 {
  80.                     str1[cnt]="LU";
  81.                     cnt++;
  82.                 }
  83.             }
  84.         }
  85.         else if(r2-r1<0 && c2-c1<0)
  86.         {
  87.             for(int i=r1,j=c1;i>=r2 && j>=c2;i--,j--)
  88.             {
  89.                 if(i==r2)
  90.                 {
  91.                     for(j;j>c2;j--)
  92.                     {
  93.                         str1[cnt]="L";
  94.                         cnt++;
  95.                     }
  96.                 }
  97.                 else if(j==c2)
  98.                 {
  99.                     for(i;i>r2;i--)
  100.                     {
  101.                         str1[cnt]="D";
  102.                         cnt++;
  103.                     }
  104.                 }
  105.                 else
  106.                 {
  107.                     str1[cnt]="LD";
  108.                     cnt++;
  109.                 }
  110.             }
  111.         }
  112.         else
  113.         {
  114.             for(int i=r1,j=c1;i>=r2 && j<=c2;i--,j++)
  115.             {
  116.                 if(i==r2)
  117.                 {
  118.                     for(j;j<c2;j++)
  119.                     {
  120.                         str1[cnt]="R";
  121.                         cnt++;
  122.                     }
  123.                 }
  124.                 else if(j==c2)
  125.                 {
  126.                     for(i;i>r2;i--)
  127.                     {
  128.                         str1[cnt]="D";
  129.                         cnt++;
  130.                     }
  131.                 }
  132.                 else
  133.                 {
  134.                     str1[cnt]="RD";
  135.                     cnt++;
  136.                 }
  137.             }
  138.         }
  139.     }
  140.     cout<<cnt<<endl;
  141.     for(int i=0;i<cnt;i++)cout<<str1[i]<<endl;
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement