P9840 [ICPC2021 Nanjing R] Oops, It‘s Yesterday Twice More题解

[ICPC2021 Nanjing R] Oops, It’s Yesterday Twice More

传送门

题面翻译

有一张 n × n n\times n n×n 的网格图,每个格子上都有一只袋鼠。对于一只在 ( i , j ) (i,j) (i,j) 的袋鼠,有下面四个按钮:

  • 按钮 U:如果 i > 1 i>1 i>1,移动到 ( i − 1 , j ) (i-1,j) (i1,j),否则,原地不动;
  • 按钮 D:如果 i < n i<n i<n,移动到 ( i + 1 , j ) (i+1,j) (i+1,j),否则,原地不动;
  • 按钮 L:如果 j > 1 j>1 j>1,移动到 ( i , j − 1 ) (i,j-1) (i,j1),否则,原地不动;
  • 按钮 R:如果 j < n j<n j<n,移动到 ( i , j + 1 ) (i,j+1) (i,j+1),否则,原地不动。

每次按下按钮,都会对所有的袋鼠生效。请输出一种使得所有袋鼠最终都在 ( a , b ) (a,b) (a,b) 的操作序列,并且序列的长度小于等于 3 × ( n − 1 ) 3\times(n-1) 3×(n1)

题目描述

After the great success in 2018, 2019, and 2020, Nanjing University of Aeronautics and Astronautics (NUAA) will host the International Collegiate Programming Contest \textit{International Collegiate Programming Contest} International Collegiate Programming Contest (ICPC) Nanjing regional for the fourth time.

Team Power of Two \textbf{\textit{Power of Two}} Power of Two and team Three Hold Two \textbf{\textit{Three Hold Two}} Three Hold Two won the champion for Tsinghua University in 2018 and 2019. In 2020, team Inverted Cross \textbf{\textit{Inverted Cross}} Inverted Cross from Peking University won the champion. In 2021, there are around 700 700 700 teams including the defending champion \textbf{the defending champion} the defending champion participating in the contest. We are so excited to see who will win this year!

Although we can’t gather in Nanjing this time due to the pandemic, we should still be grateful for the hard work done by all staff and volunteers for this contest. Thank you all for your great contribution to this contest!

In the 2018 contest, problem K, Kangaroo Puzzle \textbf{\textit{Kangaroo Puzzle}} Kangaroo Puzzle, requires the contestants to construct an operation sequence for the game:

The puzzle is a grid with n n n rows and m m m columns ( 1 ≤ n , m ≤ 20 1 \le n, m \le 20 1n,m20) and there are some (at least 2 2 2) kangaroos standing in the puzzle. The player’s goal is to control them to get together. There are some walls in some cells and the kangaroos cannot enter the cells with walls. The other cells are empty. The kangaroos can move from an empty cell to an adjacent empty cell in four directions: up, down, left, and right.

There is exactly one kangaroo in every empty cell in the beginning and the player can control the kangaroos by pressing the button U, D, L, R on the keyboard. The kangaroos will move simultaneously according to the button you press.

The contestant needs to construct an operating sequence of at most 5 × 1 0 4 5 \times 10^4 5×104 steps consisting of U, D, L, R only to achieve the goal.

In the 2020 contest, problem A, Ah, It’s Yesterday Once More \textbf{\textit{Ah, It's Yesterday Once More}} Ah, It’s Yesterday Once More, requires the contestants to construct an input map to hack the following code of the problem described before:

#include <bits/stdc++.h>
using namespace std;
string s = "UDLR";
int main()
{srand(time(NULL));for (int i = 1; i <= 50000; i++) putchar(s[rand() % 4]);return 0;
}

Now in the 2021 contest, Paimon prepares another version of the problem for you. You are given a grid with n n n rows and n n n columns ( 2 ≤ n ≤ 500 2 \leq n \leq 500 2n500). All cells are empty and there is one kangaroo standing in each cell.

Similarly, you can control the kangaroos by pressing the button U, D, L, R on the keyboard. The kangaroos will move simultaneously according to the button you press. Specifically, for any kangaroo located in the cell on the i i i-th row and the j j j-th column, indicated by ( i , j ) (i,j) (i,j):

  • Button U: it will move to ( i − 1 , j ) (i-1,j) (i1,j) if i > 1 i>1 i>1. Otherwise, it will stay in the same grid.
  • Button D: it will move to ( i + 1 , j ) (i+1,j) (i+1,j) if i < n i<n i<n. Otherwise, it will stay in the same grid.
  • Button L: it will move to ( i , j − 1 ) (i,j-1) (i,j1) if j > 1 j>1 j>1. Otherwise, it will stay in the same grid.
  • Button R: it will move to ( i , j + 1 ) (i,j+1) (i,j+1) if j < n j<n j<n. Otherwise, it will stay in the same grid.

You need to construct an operating sequence consisting only of characters U, D, L, and R. After applying it, you must make sure every kangaroo will gather at the specific cell ( a , b ) (a,b) (a,b). The length of the operating sequence cannot exceed 3 ( n − 1 ) 3(n-1) 3(n1).

输入格式

There is only one test case in each test file.

The first and only line of the input contains three integers n n n, a a a, b b b ( 2 ≤ n ≤ 500 2 \leq n \leq 500 2n500, $ 1 \leq a,b \leq n$) indicating the size of the grid and the target cell.

输出格式

Output a string consisting only of characters U, D, L and R in one line. And its length mustn’t exceed 3 ( n − 1 ) 3(n-1) 3(n1). It can be proved that the answer always exists.

样例 #1

样例输入 #1

3 3 3

样例输出 #1

RRDD

样例 #2

样例输入 #2

4 3 2

样例输出 #2

DLDLDLUR

以上来自洛谷 以上来自洛谷 以上来自洛谷

解题思路

你以为开始讲解了?不,先听龙吟:原神,启动!(这是伏笔)

正片开始

题目中说 若越界则不动 若越界则不动 若越界则不动,就可以利用这个条件把所有袋鼠聚在一个角上,然后统一移动至目标格子,目标格子离哪个角更近就到哪个。

就结束了?不,然后这题有SPJ,要注意命令顺序。(十分的让人不爽(*******)(<-你猜猜我想说什么。))

AC Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, a, b;
int tmp;
inline void work() {cin >> n >> a >> b;tmp = n / 2;if (a <= tmp && b <= tmp) {for (int i = 1; i < n; i++) {cout << "U";}for (int i = 1; i < n; i++) {cout << "L";}for (int i = 1; i < a; i++) {cout << "D";}for (int i = 1; i < b; i++) {cout << "R";}}if (a <= tmp && b > tmp) {for (int i = 1; i < n; i++) {cout << "U";}for (int i = 1; i < n; i++) {cout << "R";}for (int i = 1; i < a; i++) {cout << "D";}for (int i = n; i > b; i--) {cout << "L";}}if (a > tmp && b <= tmp) {for (int i = 1; i < n; i++) {cout << "D";}for (int i = 1; i < n; i++) {cout << "L";}for (int i = n; i > a; i--) {cout << "U";}for (int i = 1; i < b; i++) {cout << "R";}}if (a > tmp && b > tmp) {for (int i = 1; i < n; i++) {cout << "D";}for (int i = 1; i < n; i++) {cout << "R";}for (int i = n; i > a; i--) {cout << "U";}for (int i = n; i > b; i--) {cout << "L";}}
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);work();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/629522.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

4、python列表Lists

列表和你可以用它们做的事情。包括索引,切片和变异! 文章目录 1.列表1.1索引1.2切片1.3列表修改1.4列表函数1.5插曲:对象1.6列表方法1.6.1列表搜索1.7Tuples元组1.列表 Python 中的 List 表示有序的值序列: In [1]: primes = [2, 3, 5, 7]我们可以把其他类型的事情列入清…

“五星卡”上新!合合信息“外国人永久居留身份证”识别产品助力金融机构提升服务效率

外国人永久居留身份证&#xff08;简称“永居证”&#xff09;&#xff0c;是国家移民管理局对符合条件的外国人批准其在境内永久居留后&#xff0c;为其签发的法定身份证件。2023年12月&#xff0c;国家移民管理局正式启用签发更趋近于居民身份证技术体系的新一版永居证&#…

【Flutter 开发实战】Dart 基础篇:List 详解

嗨&#xff0c;各位朋友们&#xff0c;欢迎来到这篇博客&#xff01;今天我们将一起踏入 Dart 语言的神奇世界&#xff0c;深入了解 Dart 中的 List 类型。不用担心&#xff0c;我会尽可能用最通俗易懂的语言&#xff0c;让你对 List 有一个更深刻的理解。 Dart 中的 List Li…

SegVol: Universal and Interactive Volumetric Medical Image Segmentation

Abstract 精确的图像分割为临床研究提供了有意义且结构良好的信息。尽管在医学图像分割方面取得了显著的进展&#xff0c;但仍然缺乏一种能够分割广泛解剖类别且易于用户交互的基础分割模型。 本文提出了一种通用的交互式体医学图像分割模型——SegVol。通过对90k个未标记的C…

kibana查看和展示es数据

本文来说下使用kibana查看和展示es数据 文章目录 数据准备查询所有文档示例kibana查看和展示es数据 数据准备 可以使用es的命令或者java程序来往&#xff0c;es进行新增数据 查询所有文档示例 在 apifox 中&#xff0c;向 ES 服务器发 GET请求 &#xff1a;http://localhost:92…

rust跟我学二:模块编写与使用

图为RUST吉祥物 大家好,我是get_local_info作者带剑书生,这里用一篇文章讲解get_local_info中模块的使用。 首先,先要了解get_local_info是什么? get_local_info是一个获取linux系统信息的rust三方库,并提供一些常用功能,目前版本0.2.4。详细介绍地址:[我的Rust库更新]g…

在 Windows 11 上通过 Autoawq 启动 Mixtral 8*7B 大语言模型

在 Windows 11 上通过 Autoawq 启动 Mixtral 8*7B 大语言模型 0. 背景1. 安装依赖2. 开发 main.py3. 运行 main.py 0. 背景 看了一些文章之后&#xff0c;今天尝试在 Windows 11 上通过 Autoawq 启动 Mixtral 8*7B 大语言模型。 1. 安装依赖 pip install torch torchvision …

Spring框架的背景学习

Spring 的前世今生 相信经历过不使用框架开发 Web 项目的 70 后、80 后都会有如此感触&#xff0c;如今的程序员开发项目太轻松了&#xff0c;基本只需要关心业务如何实现&#xff0c;通用技术问题只需要集成框架便可。早在 2007 年&#xff0c;一个基于 Java语言的开源框架正…

Opencv基础用法学习2

案例1&#xff1a;调整图片颜色 在opencv中的读图方式是BGR,常见的读图方式是RGB // opencv 调整颜色 #include "opencv2/opencv.hpp" #include <iostream>int main() {// 读取图片cv::Mat src cv::imread("./media/dog.jpg");// BGR -> Grayc…

计算机网络 网络安全

网络安全 网络安全问题概述 计算机网络面临的女全性威胁 计算机网络的通信而临两大类威胁&#xff0c;即被动攻击和主动攻击 被动攻击是指攻击者从网络上窃听他人的通信内容。通常把这类攻击称为截获。在被动攻击中&#xff0c;攻击者只是观察和分析某一个协议数据单元 PDU…

Docker-Dockerfile

DockerFile(用于构建镜像的文本) 常用的保留字 FROM #基础镜像,基于那个镜像 MAINTAINER #镜像的维护者 #构建容器需要执行的命令(RUN)支持两种格式 shell格式&#xff1a;RUN yum -y install vim #exec格式:RUN["./test.sh", "dev", "offile&qu…

4、Redis高并发分布式锁实战

引言 在分布式系统中&#xff0c;保证数据的一致性和避免竞争条件是至关重要的。分布式锁是一种常用的机制&#xff0c;而Redis作为一款高性能的内存数据库&#xff0c;提供了简单而强大的分布式锁方案。本文将深入探讨如何利用Redis高并发分布式锁来解决分布式系统中的并发控…

Armv8-R AArch32 architecture概念学习

提示 该博客主要为个人学习&#xff0c;通过阅读官网手册整理而来&#xff08;个人觉得阅读官网的英文文档非常有助于理解各个IP特性&#xff09;。若有不对之处请参考参考文档&#xff0c;以官网文档为准。阅读该文章&#xff0c;可以先查看AArch64 Exception Model学习&…

idea 安装免费Ai工具 codeium

目录 概述 ide安装 使用 chat问答 自动写代码 除此外小功能 概述 这已经是我目前用的最好免费的Ai工具了&#xff0c;当然你要是有钱最好还是用点花钱的&#xff0c;比如copilot&#xff0c;他可以在idea全家桶包括vs&#xff0c;还有c/c的vs上运行&#xff0c;还贼强&am…

C语言如何引⽤⼀个已经定义过的外部变量?

一、问题 如何引⽤⼀个已经定义过的外部变量&#xff1f; 二、解答 如果在⼀个⽂件中定义了⼀个外部变量 a&#xff0c;在另⼀个程序⽂件中再定义⼀个外部变量 a&#xff0c;就会产⽣⼀个“重复定义”的错误&#xff0c;那么怎样引⽤⼰经定义的外部变量呢&#xff1f; 引⽤被…

CentOS下用rpm安装软件时报错error: Failed dependencies

在CentOS下用rpm安装软件时会报如下错误&#xff1a; 1、安装时提示&#xff1a; [rootdb software]# rpm -ivh ksh-20120801-254.el8.x86_64.rpm warning: ksh-20120801-254.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY error: Failed depende…

力扣白嫖日记(sql)

前言 练习sql语句&#xff0c;所有题目来自于力扣&#xff08;https://leetcode.cn/problemset/database/&#xff09;的免费数据库练习题。 今日题目&#xff1a; 586.订单最多的客户 表&#xff1a;Orders 列名类型order_numberintcustomer_numberint 查找下了最多订单的…

机器人持续学习基准LIBERO系列7——计算并可视化点云

0.前置 机器人持续学习基准LIBERO系列1——基本介绍与安装测试机器人持续学习基准LIBERO系列2——路径与基准基本信息机器人持续学习基准LIBERO系列3——相机画面可视化及单步移动更新机器人持续学习基准LIBERO系列4——robosuite最基本demo机器人持续学习基准LIBERO系列5——…

用Perl采集美容化妆目标网站做一个深度调研

在Perl中编写爬虫程序涉及到几个关键步骤&#xff0c;包括使用相关的库来发送HTTP请求和解析HTML内容。首先我们要了解Perl爬虫程序编程得几大步骤&#xff1a;安装必要的Perl模块—创建一个用户代理—发送HTTP请求—解析响应内容—提取所需数据—存储或进一步处理数据。所以说…

【ChatGPT VS baidu】:提升程序员开发效率的智能助手

在现代软件开发中&#xff0c;ChatGPT&#xff08;Chat Generative Pre-trained Transformer&#xff09;作为一种基于人工智能的对话模型&#xff0c;为程序员带来了许多便利和效率提升。ChatGPT 结合了先进的自然语言处理和生成技术&#xff0c;能够理解和生成人类语言&#…