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 4487f4bd93cd8251117da96bc0ee562ba718dfe9..4d71bebd7e2998781cbf40e69f28be5d7787a01c 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 c7b43310a6795a07e8c381f42032450e85432d2a..acf0c5e2a6e82292d30e9448fed4ca9915ac635f 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 4afabdd97b020255c5b3a9434d115f66bcf988bb..9716746ee673f54b9c9fd78e89900ec846102714 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 02e4a84d62c4b0fe9cca60bba7b9799f78f1f7ed..f32a5804e292d30bedf68f62d32fb75d87e99fd9 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 db92093f23419bfee387203da34a7f6e66edb331..cedfcf0f00e6be88442803321e0f3e9f9003a245 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 f32a5804e292d30bedf68f62d32fb75d87e99fd9..02e4a84d62c4b0fe9cca60bba7b9799f78f1f7ed 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 1de65104fc926f4786a22b51e1fc9cd4b2562d3b..888edb539edb5af72d30155c914377a3de13a577 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 02e4a84d62c4b0fe9cca60bba7b9799f78f1f7ed..f32a5804e292d30bedf68f62d32fb75d87e99fd9 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