From 73a1bd59f033d60b7c1bd570499308aa0c81e14c Mon Sep 17 00:00:00 2001 From: "chenguangjian.jk" Date: Thu, 29 Jun 2023 13:05:27 +0800 Subject: [PATCH] =?UTF-8?q?robot=20move=20add=20URDL=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exercies.md" | 23 ++++++----- .../solution.java" | 39 +++++++++++++++---- .../test_cases/2.in" | 6 +-- .../test_cases/2.out" | 2 +- .../test_cases/3.in" | 6 +-- .../test_cases/3.out" | 2 +- .../test_cases/4.in" | 8 ++-- .../test_cases/4.out" | 2 +- 8 files changed, 57 insertions(+), 31 deletions(-) diff --git "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/exercies.md" "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/exercies.md" index 4487f4b..4d71beb 100644 --- "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/exercies.md" +++ "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/exercies.md" @@ -5,18 +5,21 @@ U: 向y轴正方向移动一格 R: 向x轴正方向移动一格。 -不幸的是,在 xy 平面上还有一些障碍物,他们的坐标用obstacles表示。机器猫一旦碰到障碍物就会被损毁。 +D: 向y轴负方向移动一格 +L: 向x轴负方向移动一格。 + +不幸的是,在 xy 平面上还有一些遮挡物,他们的坐标用 barriers 表示。机器猫一旦碰到遮挡物就会被损毁。 限制: 2 <= command的长度 <= 1000 command由 U,R 构成,且至少有一个 U,至少有一个 R 0 <= x <= 1e9, 0 <= y <= 1e9 -0 <= obstacles 的长度 <= 1000 -obstacles[i]不为原点或者终点 +0 <= barriers 的长度 <= 1000 +barriers[i]不为原点或者终点 ## 输入描述 -一串UR指令: command -障碍物坐标数组: obstacles +一串URDL指令: command +遮挡物坐标数组: barriers 终点坐标(x, y) ## 输出描述 返回机器猫能否完好地到达终点。如果能,返回true;否则返回false。 @@ -25,24 +28,24 @@ obstacles[i]不为原点或者终点 ## 示例 1: -输入:command = "URR", obstacles = [], x = 3, y = 2 +输入:command = "URR", barriers = [], x = 3, y = 2 输出:true 解释:U(0, 1) -> R(1, 1) -> R(2, 1) -> U(2, 2) -> R(3, 2)。 ## 示例 2: -输入:command = "URR", obstacles = [[2, 2]], x = 3, y = 2 +输入:command = "URR", barriers = [[2, 2]], x = 3, y = 2 输出:false -解释:机器猫在到达终点前会碰到(2, 2)的障碍物。 +解释:机器猫在到达终点前会碰到(2, 2)的遮挡物。 ## 示例 3: -输入:command = "URR", obstacles = [[4, 2]], x = 3, y = 2 +输入:command = "URR", barriers = [[4, 2]], x = 3, y = 2 输出:true -解释:到达终点后,再碰到障碍物也不影响返回结果。 +解释:到达终点后,再碰到遮挡物也不影响返回结果。 # 提示 无. diff --git "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/solution.java" "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/solution.java" index c7b4331..acf0c5e 100644 --- "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/solution.java" +++ "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/solution.java" @@ -1,20 +1,34 @@ 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; char[] cmd = command.toCharArray(); // 把String转为数组,方便遍历。 for (char c : cmd) { // 算出up和right各有多少个。 - if (c == 'U') dy++; - else dx++; + if (c == 'U') { // Upper + 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); // 拿到走到终点的次数。 /* 为什么isPassed要拿到走的总次数而不直接返回true或false呢 - 比如你发现有一个obstacle是经过的,那么最终答案并不一定是false, + 比如你发现有一个barrier是经过的,那么最终答案并不一定是false, 因为如果终点在这个点的前面,那么机器人根本不会走到那个点。答案是true。 */ if (ans == -1) return false; // 终点都没经过,肯定false - for (int[] obstacle : obstacles) { - int cnt = isPassed(cmd, obstacle[0], obstacle[1], dx, dy); + for (int[] barrier : barriers) { + int cnt = isPassed(cmd, barrier[0], barrier[1], dx, dy); if (cnt != -1 && cnt < ans) return false; //不等于-1,说明经过了,然后再看这个点和终点哪个次数多。ans多,说明这个点在ans前面,返回false。 } @@ -29,11 +43,20 @@ class Solution { dy *= round; // 在第x-1或y-1层时的位置。 if (dx == x && dy == y) return cnt; // 正好就是要找的点,直接返回。 for (char c : cmd) { // 遍历第x层或y层,如果经过,那么答案一定会遍历到。 - if (c == 'U') dy++; // 要按command的顺序走 - else dx++; + if (c == 'U') { // Upper + dy++; + } else if (c == 'R') { // Right + dx++; + } else if (c == 'D') { // Down + dy--; + } else if (c == 'L') { // Left + dx--; + } cnt++; // 不要忘了每遍历一次,次数都要加1 + if (dx == x && dy == y) return cnt; // 一旦找到,直接返回所需要的次数。 } return -1; } + } diff --git "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/2.in" "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/2.in" index 4afabdd..9716746 100644 --- "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/2.in" +++ "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/2.in" @@ -1,4 +1,4 @@ -URR -[[2, 2]] -3 +RURRUL +[[1, 2]] +2 2 \ No newline at end of file diff --git "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/2.out" "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/2.out" index 02e4a84..f32a580 100644 --- "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/2.out" +++ "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/2.out" @@ -1 +1 @@ -false \ No newline at end of file +true \ No newline at end of file diff --git "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/3.in" "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/3.in" index db92093..cedfcf0 100644 --- "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/3.in" +++ "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/3.in" @@ -1,4 +1,4 @@ -URR -[[4, 2]] +UURRDLUUR +[[2, 2]] 3 -2 \ No newline at end of file +3 \ No newline at end of file diff --git "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/3.out" "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/3.out" index f32a580..02e4a84 100644 --- "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/3.out" +++ "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/3.out" @@ -1 +1 @@ -true \ No newline at end of file +false \ No newline at end of file diff --git "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/4.in" "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/4.in" index 1de6510..888edb5 100644 --- "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/4.in" +++ "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/4.in" @@ -1,4 +1,4 @@ -UUR -[[4, 2], [3, 3]] -5 -9 \ No newline at end of file +UURRDLUUR +[[1, 0]] +2 +3 \ No newline at end of file diff --git "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/4.out" "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/4.out" index 02e4a84..f32a580 100644 --- "a/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/4.out" +++ "b/exercises/chenguangjian/\344\270\255\347\255\211/\346\234\272\345\231\250\347\214\253/test_cases/4.out" @@ -1 +1 @@ -false \ No newline at end of file +true \ No newline at end of file -- GitLab