Shortest path of the king1

题目:http://codeforces.com/problemset/problem/3/A

题解:由题可得,求最短路程,那么国王走斜方向最近,而且只有四种方向,选择了就不能再变化

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    char a[5],b[5];
    cin>>a>>b;
    int x,y,z,h;
    x=a[0]-b[0];  //位移 
    y=a[1]-b[1];
    z=x<0?x=-x,'R':'L';   //x<0,那么是向右 
    h=y<0?y=-y,'U':'D';   //y<0,那么是向上 
    printf("%d",x>y?x:y);  //最小步数则是位移的大的绝对值, 
    while(x!=0||y!=0)   //若有一方不为0,都得继续走 
    {
        puts("");
        if(x)
          x--,putchar(z);
        if(y)
        y--,putchar(h);
    }
     return 0;
}
原文地址:https://www.cnblogs.com/ylrwj/p/10951745.html