【蓝桥杯】codeforce_1272B.Snow Walking Robot

题目地址:https://codeforces.com/contest/1272/problem/B

题目描述

建议阅读题目英文原题,这里只做补充说明。

有一个机器人,你需要通过一系列的指令(UDRL)来控制它的运动,现在给你一段指令,你可以通过这段指令中的全部或者一部分,来进行判断机器人是否能够走出去后再次回到原点,并且这个过程中,除了起点以外的点都不能重复经过。

解题思路:

我们只需要让机器人走出一个矩形,他就可以做到走出去后再次回到原点,并且满足除原点以外,其他点只经过一次。但是如果命令中只出现了上下,或者左右这两种指令,那么我们只能进行一次上下或者左右。其他情况,则必定是输出0。

当左右最小值为0时,前后最小值只能是0或者是1。反之一样。

当前后最小值,和左右最小值都不为0时,让它转一圈。

题中说了:请注意,您可以选择剩余指令 的任何 顺序(您不需要最小化掉期次数或任何其他类似指标)。

1
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).

1
2
3
4
5
6
7
8
# 输入
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
1
2
3
4
5
6
7
8
9
10
11
12
13
# 输出
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0

我的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
q=int(input())
lst = []
for i in range(q):
lst.append(input())

for x in lst:
u = x.count("U")
v = x.count("D")
l = x.count("L")
r = x.count("R")

a = min(u,v)
b = min(l,r)



if a<1:
b=b and 1
if b<1:
a=a and 1
print(2*(a+b))
print("U"*a+"L"*b+"D"*a+"R"*b)

别人的代码

1
2
3
4
5
6
7
8
9
10
# 没太看懂
for _ in[0]*int(input()):
u,v,*x=map(input().count,'LRUD');
a=min(u,v);
b=min(x)
if b<1:
a=a and 1
elif a<1:
b=1
print(2*(a+b),'L'*a+'U'*b+'R'*a+'D'*b)

我的代码,通过

image-20240413225742945