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. linux 内存释放命令

    我使用的是CentOS 6.5 ,由于卸载Solr 后发现内存占用挺多的,我想释放一下内存,就查阅了一些资料,分享给大家: 1.free -m  查看内存的使用情况,-m表示单位是兆 2.echo 1 ...

  2. SPRING中的线程池ThreadPoolTaskExecutor

    一.初始化 1,直接调用 ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor(); //线程池所使用的缓冲队列 p ...

  3. Oracle AWR,SQL_TRACE,10046,DBMS_PROFILER 等使用

    Oracle AWR,SQL_TRACE,10046,DBMS_PROFILER 等使用 1 AWR 工具的使用及优化 1 10g默认安装 select * from dba_hist_wr_cont ...

  4. 此上下文中不允许异步操作。启动异步操作的页必须将 Async 特性设置为 true,并且异步操作只能在 PreRenderComplete 事件之前的页上启动。

    <%@ Page Language="C#" AutoEventWireup="true" ...... Async="true" % ...

  5. List转Datable(需区分对象充当List成员和数组充当List成员两种情况)

    对象充当List成员时: /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name ...

  6. SetConsoleCtrlHandler演示

    #include "stdafx.h"#include <Windows.h> static BOOL WINAPI Handler(DWORD cntrlEvent) ...

  7. jdbcTemplate学习(四)

    前面三节讲了jdbcTemplate的使用,这一节讲解NamedParameterJdbcTemplate的使用方法: NamedParameterJdbcTemplate类是基于JdbcTempla ...

  8. eclipse怎么查看class文件(eclipse安装反编译插件)

    本人eclipse版本: Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) 步骤1:下载两个我们需要的东西 ...

  9. Yaffs2根文件系统制作

    Yaffs2根文件系统制作 环境: 交叉编译环境:4.4.6 开发平台:s3c2416 1,编译busybox 获取busybox源码busybox-1.17.2.tar (http://www.bu ...

  10. actionbar中添加searchview并监听期伸缩/打开的方法

    首先在xml中设置actionviewclass <item android:id="@+id/m1" android:title="setting" a ...