D

Problem Statement

Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis pointing right and the positive y-axis pointing up.
Currently, he is located at the point (sx,sy). In each second, he can move up, down, left or right by a distance of 1.
Here, both the x- and y-coordinates before and after each movement must be integers.
He will first visit the point (tx,ty) where sx<tx and sy<ty, then go back to the point (sx,sy), then visit the point (tx,ty) again, and lastly go back to the point (sx,sy).
Here, during the whole travel, he is not allowed to pass through the same point more than once, except the points (sx,sy) and (tx,ty).
Under this condition, find a shortest path for him.

Constraints

  • −1000≤sx<tx≤1000
  • −1000≤sy<ty≤1000
  • sx,sy,tx and ty are integers.

Input

The input is given from Standard Input in the following format:

sx sy tx ty

Output

Print a string S that represents a shortest path for Dolphin.
The i-th character in S should correspond to his i-th movement.
The directions of the movements should be indicated by the following characters:

  • U: Up
  • D: Down
  • L: Left
  • R: Right

If there exist multiple shortest paths under the condition, print any of them.

Sample Input 1

0 0 1 2

Sample Output 1

UURDDLLUUURRDRDDDLLU

One possible shortest path is:

  • Going from (sx,sy) to (tx,ty) for the first time: (0,0) → (0,1) → (0,2) → (1,2)
  • Going from (tx,ty) to (sx,sy) for the first time: (1,2) → (1,1) → (1,0) → (0,0)
  • Going from (sx,sy) to (tx,ty) for the second time: (0,0) → (−1,0) → (−1,1) → (−1,2)→ (−1,3) → (0,3) → (1,3) → (1,2)
  • Going from (tx,ty) to (sx,sy) for the second time: (1,2) → (2,2) → (2,1) → (2,0) → (2,−1) → (1,−1) → (0,−1) → (0,0)

Sample Input 2

-2 -2 1 1

Sample Output 2

UURRURRDDDLLDLLULUUURRURRDDDLLDL

题解:简单的模拟,当时把它想成最短路的问题,自己真的很菜呀!记得比赛过后有人说这次的题目都是水题,我当时还不信,不过现在我信了。不是因为题难而是因为自己太菜了,可能是因为当时自己把它想成一个图论的问题了吧,想到是图论然后自己就没有再往后想。主要是自己被当时的一道题给卡住了,然后就一直在想那道题,可是最后那道题还是没有想到要怎么写。吸取教训下次一定不要在一道题上卡太长时间,图论的知识还要补一下。加油!

题解:因为题中有提到sx < tx and sy < ty, 所以应该能够想到第一次走的路线不论怎么走最后都可以通过平移转换成一个矩形。第二次走的路径在原来的基础上再向外加一个单位即可。

AC代码:

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int sx, sy, tx, ty;
 6     while(~scanf("%d%d%d%d", &sx, &sy, &tx, &ty))
 7     {
 8         for(int i = sx+1; i <= tx; i++)   printf("R");
 9         for(int i = sy+1; i <= ty; i++)   printf("U");
10         for(int i = tx-1; i >= sx; i--)   printf("L");
11         for(int i = ty-1; i >= sy; i--)   printf("D");
12 
13         printf("D");
14         for(int i = sx+1; i <= tx+1; i++)   printf("R");
15         for(int i = sy; i <= ty; i++)   printf("U");
16         printf("LU");
17         for(int i = tx-1; i >= sx-1; i--)   printf("L");
18         for(int i = ty; i >= sy; i--)   printf("D");
19         printf("R
");
20     }
21 
22     return 0;
23 }
View Code
永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/8561151.html