题解

矩阵中的路径

http://www.nowcoder.com/questionTerminal/c61c6999eecb4b8f88a98f66b273a3cc

题解

 public int[][] visit;
    public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
      visit = new int[rows][cols];
      char[][] array = new char[rows][cols];
      for (int i = 0; i < rows ; i++) {
          for(int j = 0; j < cols; j++) {
              array[i][j] = matrix[i*cols + j];
          }
      }
        for (int i = 0; i < rows ; i++) {
            for(int j = 0; j < cols; j++) {
               if(find(array,rows,cols,str,i,j,0)){
                   return  true;
               }
            }
        }
      return false;
    }
    public boolean find(char[][] array, int rows, int cols, char[] str, int rpos,int cpos, int spos) {

        if(spos >= str.length) {
            return  true;
        }
       if(rpos < 0 || cpos < 0 || rpos >= rows || cpos >= cols || array[rpos][cpos] != str[spos] || visit[rpos][cpos] == 1) {

          return false;
       }
       visit[rpos][cpos] = 1;
       boolean isSunc =  find( array,   rows,  cols, str,  rpos+1, cpos, spos+1)
               || find( array,   rows,  cols, str,  rpos , cpos+1, spos+1)
               || find( array,   rows,  cols, str,  rpos-1, cpos, spos+1)
               || find( array,   rows,  cols, str,  rpos , cpos-1, spos+1);
       visit[rpos][cpos] = 0;
       return isSunc;
    }
全部评论
为什么把标志数组置1又置0啊 visit[rpos][cpos] = 1; boolean isSunc = find( array, rows, cols, str, rpos+1, cpos, spos+1) || find( array, rows, cols, str, rpos , cpos+1, spos+1) || find( array, rows, cols, str, rpos-1, cpos, spos+1) || find( array, rows, cols, str, rpos , cpos-1, spos+1); visit[rpos][cpos] = 0;
点赞 回复 分享
发布于 2020-08-26 11:10
大佬,我想问一下array[i][j] = matrix[i*cols + j]是什么意思,原谅我渣渣看不懂
点赞 回复 分享
发布于 2020-07-23 15:01

相关推荐

矫健的闭门羹烹饪师又...:本人双非本,在鹅厂测开实习,你这个简历上写的这两个项目的技术栈都差不多,能够让面试官去延伸去问的八股除了redis就再没啥了,建议项目这边可以再改改,然后专业技能那块的话,感觉linux和测试工具可以分开写,毕竟不是干一件事的,反正没实习的基础上面试就深挖项目和八股,好好卷吧
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
测试糕手手:社会第一课,随便吹牛逼,直接说四个月,别老实。老实人只会被欺负
点赞 评论 收藏
分享
评论
22
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务