提交 73a1bd59 编写于 作者: C chenguangjian.jk

robot move add URDL指令

上级 ba5aefe3
...@@ -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 <= obstacles 的长度 <= 1000 0 <= barriers 的长度 <= 1000
obstacles[i]不为原点或者终点 barriers[i]不为原点或者终点
## 输入描述 ## 输入描述
一串UR指令: command 一串URDL指令: command
障碍物坐标数组: obstacles 遮挡物坐标数组: barriers
终点坐标(x, y) 终点坐标(x, y)
## 输出描述 ## 输出描述
返回机器猫能否完好地到达终点。如果能,返回true;否则返回false。 返回机器猫能否完好地到达终点。如果能,返回true;否则返回false。
...@@ -25,24 +28,24 @@ obstacles[i]不为原点或者终点 ...@@ -25,24 +28,24 @@ obstacles[i]不为原点或者终点
## 示例 1: ## 示例 1:
输入:command = "URR", obstacles = [], x = 3, y = 2 输入:command = "URR", barriers = [], 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", obstacles = [[2, 2]], x = 3, y = 2 输入:command = "URR", barriers = [[2, 2]], x = 3, y = 2
输出:false 输出:false
解释:机器猫在到达终点前会碰到(2, 2)的障碍物。 解释:机器猫在到达终点前会碰到(2, 2)的遮挡物。
## 示例 3: ## 示例 3:
输入:command = "URR", obstacles = [[4, 2]], x = 3, y = 2 输入:command = "URR", barriers = [[4, 2]], x = 3, y = 2
输出:true 输出:true
解释:到达终点后,再碰到障碍物也不影响返回结果。 解释:到达终点后,再碰到遮挡物也不影响返回结果。
# 提示 # 提示
无. 无.
......
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 : obstacles) { for (int[] barrier : barriers) {
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;
} }
} }
URR RURRUL
[[2, 2]] [[1, 2]]
3 2
2 2
\ No newline at end of file
false true
\ No newline at end of file \ No newline at end of file
URR UURRDLUUR
[[4, 2]] [[2, 2]]
3
3 3
\ No newline at end of file
2
\ No newline at end of file
true false
\ No newline at end of file \ No newline at end of file
UUR UURRDLUUR
[[4, 2], [3, 3]] [[1, 0]]
5 2
9 3
\ No newline at end of file \ No newline at end of file
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.
先完成此消息的编辑!
想要评论请 注册