OJ日常 | 回溯——矩阵中的路径、机器人的运动范围
2018-03-23 01:17
441 查看
题目1:矩阵中的路径
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串”bcced”的路径,但是矩阵中不包含”abcb”路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。题目1:一个答案
重温这道题,一句mmp。具体解析可以看Algorithm第一篇博文卡在判断语句的先后顺序上,把str[z]!=matrix[k]str[z]!=matrix[k]现在了前面,导致OutOfBound
放在后面没事,因为||的就进原则,之前语句false后,直接返回false,不再对后面语句进行判断。
这段代码在自己IDE上跑,跑是能跑,就是有这样一个error
( i<0 || i>=rows || j<0 || j>=cols || flag[k]==1 || str[z]!=matrix[k] )
public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
{
int[] flag = new int[rows*cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(explorer(matrix, rows, cols, str, flag, i, j, 0))
return true;
}
}
return false;
}
public boolean explorer(char[] matrix, int rows, int cols, char[] str, int[] flag, int i, int j, int z){
int k=i*cols+j;
if( i<0 || i>=rows || j<0 || j>=cols || flag[k]==1 || str[z]!=matrix[k] )return false;
if(z==str.length-1) return true;
flag[k]=1;
if(explorer(matrix, rows, cols, str, flag, i+1, j, z+1)||
explorer(matrix, rows, cols, str, flag, i, j+1, z+1)||
explorer(matrix, rows, cols, str, flag, i-1, j, z+1)||
explorer(matrix, rows, cols, str, flag, i, j-1, z+1)){
return true;
}
flag[k]=0;
return false;
}
}
题目2:机器人的运动范围
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?题目2:一个答案
夜深,明日再战
起床了, 华丽的分割线
这个问题容易绊住的地方时,到达多少格子这句话的理解,这个格子不是问起点到终点距离,而是从起点开始,开枝散叶最大的覆盖面积。
eclipse上运行正确,牛客运行不正确。
这是eclipse上正确
public class Solution { static int count = 0; public int movingCount(int threshold, int rows, int cols) { int[] flag = new int[rows*cols]; explorer(threshold, rows, cols, 0, 0, flag); return count; } public void explorer(int threshold, int rows, int cols, int i, int j, int[] flag){ int k = i*rows + j; int z = i%10+i/10+j%10+j/10; if(i<0 || i>=rows || j<0 || j>=cols || flag[k]==1 || z>threshold) return; ++count; flag[k]=1; explorer(threshold, rows, cols, i+1, j, flag); explorer(threshold, rows, cols, i, j+1, flag); explorer(threshold, rows, cols, i-1, j, flag); explorer(threshold, rows, cols, i, j-1, flag); return; } }
牛客也正确的。copy别人的代码
//链接:https://www.nowcoder.com/questionTerminal/6e5207314b5241fb83f2329e89fdecc8 //来源:牛客网 public class Solution { public int movingCount(int threshold, int rows, int cols) { int flag[][] = new int[rows][cols]; //记录是否已经走过 return helper(0, 0, rows, cols, flag, threshold); } private int helper(int i, int j, int rows, int cols, int[][] flag, int threshold) { if (i < 0 || i >= rows || j < 0 || j >= cols || numSum(i) + numSum(j) > threshold || flag[i][j] == 1) return 0; flag[i][j] = 1; return helper(i - 1, j, rows, cols, flag, threshold) + helper(i + 1, j, rows, cols, flag, threshold) + helper(i, j - 1, rows, cols, flag, threshold) + helper(i, j + 1, rows, cols, flag, threshold) + 1; } private int numSum(int i) { int sum = 0; do{ sum += i%10; }while((i = i/10) > 0); return sum; } }
相关文章推荐
- 回溯——矩阵中路径问题(0,1矩阵中最优路径、矩阵中的路径、机器人的运动范围)
- 回溯——矩阵中路径问题(0,1矩阵中最优路径、矩阵中的路径、机器人的运动范围)
- 回溯——矩阵中路径问题(0,1矩阵中最优路径、矩阵中的路径、机器人的运动范围)
- 回溯——矩阵中路径问题(0,1矩阵中最优路径、矩阵中的路径、机器人的运动范围)
- 回溯——矩阵中路径问题(0,1矩阵中最优路径、矩阵中的路径、机器人的运动范围)
- 回溯——矩阵中路径问题(0,1矩阵中最优路径、矩阵中的路径、机器人的运动范围)
- 回溯——矩阵中路径问题(0,1矩阵中最优路径、矩阵中的路径、机器人的运动范围)
- 矩阵中的路径(dfs深搜), 机器人的运动范围(dfs深搜)
- 【剑指offer】第十二题(矩阵中的路径)和 第十三题(机器人的运动范围)
- 剑指offer 机器人的运动范围 矩阵中的路径 滑动窗口最大值 次数超过一半的数 连续子数组的最大和
- 剑指offer——机器人的运动范围(回溯)
- 剑指第13题:机器人的运动范围------回溯法
- [剑指Offer题集] 机器人的运动范围(Python)
- 机器人的运动范围
- 面试题:机器人的运动范围
- 《剑指offer》面试题67 机器人的运动范围
- 机器人的运动范围
- 机器人的运动范围
- 回溯法 机器人的运动范围
- 面试题67. 机器人的运动范围