Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
禅与计算机程序设计艺术
contest_exercises_template
提交
73a1bd59
C
contest_exercises_template
项目概览
禅与计算机程序设计艺术
/
contest_exercises_template
该项目与 Fork 源项目分叉
Fork自
CSDN 技术社区 / contest_exercises_template
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
contest_exercises_template
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
73a1bd59
编写于
6月 29, 2023
作者:
C
chenguangjian.jk
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
robot move add URDL指令
上级
ba5aefe3
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
63 addition
and
36 deletion
+63
-36
exercises/chenguangjian/中等/机器猫/exercies.md
exercises/chenguangjian/中等/机器猫/exercies.md
+13
-10
exercises/chenguangjian/中等/机器猫/solution.java
exercises/chenguangjian/中等/机器猫/solution.java
+31
-8
exercises/chenguangjian/中等/机器猫/test_cases/2.in
exercises/chenguangjian/中等/机器猫/test_cases/2.in
+4
-3
exercises/chenguangjian/中等/机器猫/test_cases/2.out
exercises/chenguangjian/中等/机器猫/test_cases/2.out
+2
-2
exercises/chenguangjian/中等/机器猫/test_cases/3.in
exercises/chenguangjian/中等/机器猫/test_cases/3.in
+4
-4
exercises/chenguangjian/中等/机器猫/test_cases/3.out
exercises/chenguangjian/中等/机器猫/test_cases/3.out
+2
-2
exercises/chenguangjian/中等/机器猫/test_cases/4.in
exercises/chenguangjian/中等/机器猫/test_cases/4.in
+5
-5
exercises/chenguangjian/中等/机器猫/test_cases/4.out
exercises/chenguangjian/中等/机器猫/test_cases/4.out
+2
-2
未找到文件。
exercises/chenguangjian/中等/机器猫/exercies.md
浏览文件 @
73a1bd59
...
@@ -5,18 +5,21 @@
...
@@ -5,18 +5,21 @@
U: 向y轴正方向移动一格
U: 向y轴正方向移动一格
R: 向x轴正方向移动一格。
R: 向x轴正方向移动一格。
不幸的是,在 xy 平面上还有一些障碍物,他们的坐标用obstacles表示。机器猫一旦碰到障碍物就会被损毁。
D: 向y轴负方向移动一格
L: 向x轴负方向移动一格。
不幸的是,在 xy 平面上还有一些遮挡物,他们的坐标用 barriers 表示。机器猫一旦碰到遮挡物就会被损毁。
限制:
限制:
2 <= command的长度 <= 1000
2 <= command的长度 <= 1000
command由 U,R 构成,且至少有一个 U,至少有一个 R
command由 U,R 构成,且至少有一个 U,至少有一个 R
0 <= x <= 1e9, 0 <= y <= 1e9
0 <= x <= 1e9, 0 <= y <= 1e9
0 <=
obstacle
s 的长度 <= 1000
0 <=
barrier
s 的长度 <= 1000
obstacle
s[i]不为原点或者终点
barrier
s[i]不为原点或者终点
## 输入描述
## 输入描述
一串UR指令: command
一串UR
DL
指令: command
障碍物坐标数组: obstacle
s
遮挡物坐标数组: barrier
s
终点坐标(x, y)
终点坐标(x, y)
## 输出描述
## 输出描述
返回机器猫能否完好地到达终点。如果能,返回true;否则返回false。
返回机器猫能否完好地到达终点。如果能,返回true;否则返回false。
...
@@ -25,24 +28,24 @@ obstacles[i]不为原点或者终点
...
@@ -25,24 +28,24 @@ obstacles[i]不为原点或者终点
## 示例 1:
## 示例 1:
输入:command = "URR",
obstacle
s = [], x = 3, y = 2
输入:command = "URR",
barrier
s = [], x = 3, y = 2
输出:true
输出:true
解释:U(0, 1) -> R(1, 1) -> R(2, 1) -> U(2, 2) -> R(3, 2)。
解释:U(0, 1) -> R(1, 1) -> R(2, 1) -> U(2, 2) -> R(3, 2)。
## 示例 2:
## 示例 2:
输入:command = "URR",
obstacle
s = [[2, 2]], x = 3, y = 2
输入:command = "URR",
barrier
s = [[2, 2]], x = 3, y = 2
输出:false
输出:false
解释:机器猫在到达终点前会碰到(2, 2)的
障碍
物。
解释:机器猫在到达终点前会碰到(2, 2)的
遮挡
物。
## 示例 3:
## 示例 3:
输入:command = "URR",
obstacle
s = [[4, 2]], x = 3, y = 2
输入:command = "URR",
barrier
s = [[4, 2]], x = 3, y = 2
输出:true
输出:true
解释:到达终点后,再碰到
障碍
物也不影响返回结果。
解释:到达终点后,再碰到
遮挡
物也不影响返回结果。
# 提示
# 提示
无.
无.
...
...
exercises/chenguangjian/中等/机器猫/solution.java
浏览文件 @
73a1bd59
class
Solution
{
class
Solution
{
public
boolean
robot
(
String
command
,
int
[][]
obstacles
,
int
x
,
int
y
)
{
public
boolean
robot
(
String
command
,
int
[][]
barriers
,
int
x
,
int
y
)
{
System
.
out
.
println
();
System
.
out
.
println
(
command
);
System
.
out
.
println
(
Arrays
.
deepToString
(
barriers
));
System
.
out
.
println
(
x
);
System
.
out
.
println
(
y
);
int
dx
=
0
,
dy
=
0
;
int
dx
=
0
,
dy
=
0
;
char
[]
cmd
=
command
.
toCharArray
();
// 把String转为数组,方便遍历。
char
[]
cmd
=
command
.
toCharArray
();
// 把String转为数组,方便遍历。
for
(
char
c
:
cmd
)
{
// 算出up和right各有多少个。
for
(
char
c
:
cmd
)
{
// 算出up和right各有多少个。
if
(
c
==
'U'
)
dy
++;
if
(
c
==
'U'
)
{
// Upper
else
dx
++;
dy
++;
}
else
if
(
c
==
'R'
)
{
// Right
dx
++;
}
else
if
(
c
==
'D'
)
{
// Down
dy
--;
}
else
if
(
c
==
'L'
)
{
// Left
dx
--;
}
}
}
int
ans
=
isPassed
(
cmd
,
x
,
y
,
dx
,
dy
);
// 拿到走到终点的次数。
int
ans
=
isPassed
(
cmd
,
x
,
y
,
dx
,
dy
);
// 拿到走到终点的次数。
/*
/*
为什么isPassed要拿到走的总次数而不直接返回true或false呢
为什么isPassed要拿到走的总次数而不直接返回true或false呢
比如你发现有一个
obstacle
是经过的,那么最终答案并不一定是false,
比如你发现有一个
barrier
是经过的,那么最终答案并不一定是false,
因为如果终点在这个点的前面,那么机器人根本不会走到那个点。答案是true。
因为如果终点在这个点的前面,那么机器人根本不会走到那个点。答案是true。
*/
*/
if
(
ans
==
-
1
)
return
false
;
// 终点都没经过,肯定false
if
(
ans
==
-
1
)
return
false
;
// 终点都没经过,肯定false
for
(
int
[]
obstacle
:
obstacle
s
)
{
for
(
int
[]
barrier
:
barrier
s
)
{
int
cnt
=
isPassed
(
cmd
,
obstacle
[
0
],
obstacle
[
1
],
dx
,
dy
);
int
cnt
=
isPassed
(
cmd
,
barrier
[
0
],
barrier
[
1
],
dx
,
dy
);
if
(
cnt
!=
-
1
&&
cnt
<
ans
)
return
false
;
if
(
cnt
!=
-
1
&&
cnt
<
ans
)
return
false
;
//不等于-1,说明经过了,然后再看这个点和终点哪个次数多。ans多,说明这个点在ans前面,返回false。
//不等于-1,说明经过了,然后再看这个点和终点哪个次数多。ans多,说明这个点在ans前面,返回false。
}
}
...
@@ -29,11 +43,20 @@ class Solution {
...
@@ -29,11 +43,20 @@ class Solution {
dy
*=
round
;
// 在第x-1或y-1层时的位置。
dy
*=
round
;
// 在第x-1或y-1层时的位置。
if
(
dx
==
x
&&
dy
==
y
)
return
cnt
;
// 正好就是要找的点,直接返回。
if
(
dx
==
x
&&
dy
==
y
)
return
cnt
;
// 正好就是要找的点,直接返回。
for
(
char
c
:
cmd
)
{
// 遍历第x层或y层,如果经过,那么答案一定会遍历到。
for
(
char
c
:
cmd
)
{
// 遍历第x层或y层,如果经过,那么答案一定会遍历到。
if
(
c
==
'U'
)
dy
++;
// 要按command的顺序走
if
(
c
==
'U'
)
{
// Upper
else
dx
++;
dy
++;
}
else
if
(
c
==
'R'
)
{
// Right
dx
++;
}
else
if
(
c
==
'D'
)
{
// Down
dy
--;
}
else
if
(
c
==
'L'
)
{
// Left
dx
--;
}
cnt
++;
// 不要忘了每遍历一次,次数都要加1
cnt
++;
// 不要忘了每遍历一次,次数都要加1
if
(
dx
==
x
&&
dy
==
y
)
return
cnt
;
// 一旦找到,直接返回所需要的次数。
if
(
dx
==
x
&&
dy
==
y
)
return
cnt
;
// 一旦找到,直接返回所需要的次数。
}
}
return
-
1
;
return
-
1
;
}
}
}
}
exercises/chenguangjian/中等/机器猫/test_cases/2.in
浏览文件 @
73a1bd59
URR
RURRUL
[[
2
, 2]]
[[
1
, 2]]
3
2
2
2
\ No newline at end of file
exercises/chenguangjian/中等/机器猫/test_cases/2.out
浏览文件 @
73a1bd59
false
true
\ No newline at end of file
\ No newline at end of file
exercises/chenguangjian/中等/机器猫/test_cases/3.in
浏览文件 @
73a1bd59
URR
UURRDLUUR
[[4, 2]]
[[2, 2]]
3
3
3
\ No newline at end of file
2
\ No newline at end of file
exercises/chenguangjian/中等/机器猫/test_cases/3.out
浏览文件 @
73a1bd59
true
false
\ No newline at end of file
\ No newline at end of file
exercises/chenguangjian/中等/机器猫/test_cases/4.in
浏览文件 @
73a1bd59
UUR
UURRDLUUR
[[4, 2], [3, 3]]
[[1, 0]]
5
2
9
3
\ No newline at end of file
\ No newline at end of file
exercises/chenguangjian/中等/机器猫/test_cases/4.out
浏览文件 @
73a1bd59
false
true
\ No newline at end of file
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录