2022-3-9 剑指offer day27

题1:

JZ12 矩阵中的路径

描述

请设计一个函数,用来判断在一个n乘m的矩阵中是否存在一条包含某长度为len的字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 \begin{bmatrix} a & b & c &e \\ s & f & c & s \\ a & d & e& e\\ \end{bmatrix}\quadasabfdcceese 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
数据范围:0 \le n,m \le 20\0n,m20 ,1\le len \le 25\1len25 
 
复制代码
 1 import java.util.*;
 2 
 3 
 4 public class Solution {
 5     /**
 6      * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 7      *
 8      * 
 9      * @param matrix char字符型二维数组 
10      * @param word string字符串 
11      * @return bool布尔型
12      */
13     boolean ans=false;
14     String comp;
15     int[][] dir={{-1,0},{1,0},{0,1},{0,-1}};
16     public boolean hasPath (char[][] matrix, String word) {
17         // write code here
18         int len=word.length(),m=matrix.length,n=matrix[0].length;
19         comp=word;
20         StringBuilder sb=new StringBuilder();
21         boolean[][] visited=new boolean[m][n];
22         for (int i=0;i<m;i++){
23             for (int j=0;j<n;j++) {
24                 if (ans) return ans;
25                 //剪枝 开头字母相同再搜索
26                 if (matrix[i][j]==word.charAt(0)){
27                     visited[i][j]=true;
28                     sb.append(matrix[i][j]);
29                     dfs(i,j,matrix,len,sb,visited);
30                     visited[i][j]=false;
31                     sb.deleteCharAt(sb.length()-1);
32                 }
33 
34             }
35         }
36         return ans;
37     }
38     
39     
40     public void dfs(int x,int y,char[][] arr,int len,StringBuilder sb,boolean[][] visited){
41         if (sb.length()==len) {
42             if (sb.toString().equals(comp)) ans=true;
43             return;
44         }
45         if (ans) return;
46         int m=arr.length,n=arr[0].length;
47         for (int[] t:dir){
48             int dx=x+t[0];
49             int dy=y+t[1];
50             // 剪枝 当前坐标合法且相同再dfs
51             if (dx>=0&&dy>=0&&dx<m&&dy<n&&!visited[dx][dy]&&arr[dx][dy]==comp.charAt(sb.length())){
52                 sb.append(arr[dx][dy]);
53                 visited[dx][dy]=true;
54                 dfs(dx,dy,arr,len,sb,visited);
55                 sb.deleteCharAt(sb.length()-1);
56                 visited[dx][dy]=false;
57             }
58         }
59         
60     }
61 }
复制代码

思路:深度优先搜索。要剪枝  不然时间会超时。

 

题2:

JZ13 机器人的运动范围

 

描述

地上有一个 rows 行和 cols 列的方格。坐标从 [0,0] 到 [rows-1,cols-1] 。一个机器人从坐标 [0,0] 的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于 threshold 的格子。 例如,当 threshold 为 18 时,机器人能够进入方格   [35,37] ,因为 3+5+3+7 = 18。但是,它不能进入方格 [35,38] ,因为 3+5+3+8 = 19 。请问该机器人能够达到多少个格子?
 
数据范围: 0 \le threshold \le 15 \0threshold15  ,1 \le rows,cols \le 100 \1rows,cols100 
 
进阶:空间复杂度 O(nm) \O(nm)  ,时间复杂度 O(nm) \O(nm) 
复制代码
 1 import java.util.*;
 2 public class Solution {
 3     public int movingCount(int threshold, int rows, int cols) {
 4         boolean[][] visited=new boolean[rows][cols];
 5         visited[0][0]=true;
 6         int ans=1;
 7         int[][] dir={{1,0},{-1,0},{0,1},{0,-1}};
 8         Queue<int[]> queue=new LinkedList<>();
 9         queue.add(new int[]{0,0});
10         while (!queue.isEmpty()){
11             int[] t=queue.poll();
12             for (int[] p:dir){
13                 int dx=p[0]+t[0];
14                 int dy=p[1]+t[1];
15                 if (dx>=0&&dy>=0&&dx<rows&&dy<cols&&!visited[dx][dy]&&check(dx,dy,threshold)){
16                     queue.offer(new int[]{dx,dy});
17                     ans++;
18                     visited[dx][dy]=true;
19                 }
20             }
21         }
22         
23         return ans;
24     }
25     
26     public boolean check(int x,int y, int sum){
27        int s=0;
28        while (x>0) {
29            s+=x%10;
30            x/=10;
31        }
32           while (y>0) {
33            s+=y%10;
34            y/=10;
35        }
36         if (s<=sum) return true;
37         return false;
38     }
39 }
复制代码

思路:  dfs跟bfs都能写,写了一个BFS版本的。

posted on   阿ming  阅读(23)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示