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代码:

 #include<stdio.h>

 int main()
{
int sx, sy, tx, ty;
while(~scanf("%d%d%d%d", &sx, &sy, &tx, &ty))
{
for(int i = sx+; i <= tx; i++) printf("R");
for(int i = sy+; i <= ty; i++) printf("U");
for(int i = tx-; i >= sx; i--) printf("L");
for(int i = ty-; i >= sy; i--) printf("D"); printf("D");
for(int i = sx+; i <= tx+; i++) printf("R");
for(int i = sy; i <= ty; i++) printf("U");
printf("LU");
for(int i = tx-; i >= sx-; i--) printf("L");
for(int i = ty; i >= sy; i--) printf("D");
printf("R\n");
} return ;
}

D - Back and Forth(模拟)的更多相关文章

  1. App开发:模拟服务器数据接口 - MockApi

    为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...

  2. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  3. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  4. HTML 事件(四) 模拟事件操作

    本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4.  ...

  5. 模拟AngularJS之依赖注入

    一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...

  6. webapp应用--模拟电子书翻页效果

    前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...

  7. javascript动画系列第一篇——模拟拖拽

    × 目录 [1]原理介绍 [2]代码实现 [3]代码优化[4]拖拽冲突[5]IE兼容 前面的话 从本文开始,介绍javascript动画系列.javascript本身是具有原生拖放功能的,但是由于兼容 ...

  8. C++ 事件驱动型银行排队模拟

    最近重拾之前半途而废的C++,恰好看到了<C++ 实现银行排队服务模拟>,但是没有实验楼的会员,看不到具体的实现,正好用来作为练习. 模拟的是银行的排队叫号系统,所有顾客以先来后到的顺序在 ...

  9. MSYS2——Windows平台下模拟linux环境的搭建

    最近从MSYS1.0迁移到了MSYS2.0,简单讲,MSYS2.0功能更强大,其环境模拟更加符合linux.虽然本身来自cygwin,但其集成了pacman软件管理工具,很有linux范,并且可以直接 ...

  10. trigger事件模拟

    事件模拟trigger 在操作DOM元素中,大多数事件都是用户必须操作才会触发事件,但有时,需要模拟用户的操作,来达到效果. 需求:页面初始化时触发搜索事件并获取input控件值,并打印输出(效果图如 ...

随机推荐

  1. 配置Nginx实现负载均衡

    在关于高并发负载均衡一文中已经提到,企业在解决高并发问题时,一般有两个方向的处理策略,软件.硬件,硬件上添加负载均衡器分发大量请求,软件上可在高并发瓶颈处:数据库+web服务器两处添加解决方案,其中w ...

  2. lrzsz-串口传输文件

    二.编译安装 1.解压文件,进入目录     tar –zxvf lrzsz-0.12.20.tar.bz        cd / lrzsz-0.12.20 ./configure  2../con ...

  3. c# 遍历目录

    public static List<string> TraverseDirector(string dir, bool isTraveSubDirFlag, bool isFilterS ...

  4. 最新版CocoaPods的安装流程

    1.移除现有Ruby默认源 $gem sources --remove https://rubygems.org/ 2.使用新的源 $gem sources -a https://ruby.taoba ...

  5. 数据校验(2)--demo1---bai

    input_score.jsp <%@ page language="java" import="java.util.*" pageEncoding=&q ...

  6. Beautiful Soup 4.2.0

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 快速开始 pip install beaut ...

  7. mybaits中date类型显示时分秒(orcle数据库)

    <insert id="insert" parameterType="daSysLoginLog"> insert into DA_SYS_LOGI ...

  8. hive删除表报错:Specified key was too long; max key length&nb

    我是在hive删除表的时候出现这个错误的,看到这个错误应该就知道是字符集错误. 但是我用​ alter database hive character set latin1; 这个命令将其改成拉丁之后 ...

  9. java之链表

    链表是一种物理存储单元上非连续.非顺序的存储结构. 链表是由那几个部分组成的呢? 是由N个节点组成的       每一个节点分为两部分:                         1.数据域  ...

  10. memset,memcpy,strcpy的使用与区别

    1.memset 原型:   extern void *memset(void *buffer, int c, int count); 功能:   把buffer所指内存区域的前count个字节设置成 ...