剑指 Offer 12. 矩阵中的路径

剑指 Offer 12. 矩阵中的路径

  1. class Solution {
  2. int m,n;
  3. char[] c;
  4. boolean[][] visited;
  5. char[][] board;
  6. int[][] directions=new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
  7. public boolean exist(char[][] board, String word) {
  8. this.board=board;
  9. m=board.length;
  10. n=board[0].length;
  11. c=word.toCharArray();
  12. visited=new boolean[m][n];
  13. for(int i=0;i<m;i++){
  14. for(int j=0;j<n;j++){
  15. if(board[i][j]==c[0]){
  16. visited[i][j]=true;
  17. if(backtrack(i,j,1)) return true;
  18. visited[i][j]=false;;
  19. }
  20. }
  21. }
  22. return false;
  23. }
  24. private boolean backtrack(int i,int j,int next){
  25. if(next==c.length) return true;
  26. for(int[] dir:directions){
  27. int x=i+dir[0];
  28. int y=j+dir[1];
  29. if(x>=0&&y>=0&&x<m&&y<n&&c[next]==board[x][y]&&!visited[x][y]){
  30. visited[x][y]=true;
  31. if(backtrack(x, y, next+1)) return true;
  32. visited[x][y]=false;
  33. }
  34. }
  35. return false;
  36. }
  37. }

剑指 Offer 13. 机器人的运动范围

剑指 Offer 13. 机器人的运动范围

  1. class Solution {
  2. boolean[][] reachable;
  3. boolean[][] checked;
  4. int m,n,k;
  5. public int movingCount(int m, int n, int k) {
  6. this.m=m;
  7. this.n=n;
  8. this.k=k;
  9. reachable =new boolean[m][n];
  10. checked=new boolean[m][n];
  11. dfs(0,0);
  12. int count=0;
  13. for(int i=0;i<m;i++){
  14. for(int j=0;j<n;j++){
  15. if(reachable[i][j]) count++;
  16. }
  17. }
  18. return count;
  19. }
  20. private void dfs(int i,int j){
  21. if(i<0||j<0||i>=m||j>=n||checked[i][j]){
  22. return;
  23. }
  24. checked[i][j]=true;
  25. if(calculate(i, j)<=k){
  26. reachable[i][j]=true;
  27. dfs(i+1,j);
  28. dfs(i-1,j);
  29. dfs(i,j+1);
  30. dfs(i,j-1);
  31. }else{
  32. reachable[i][j]=false;
  33. }
  34. }
  35. private int calculate(int i,int j){
  36. int count=0;
  37. while(i>0){
  38. count+=i%10;
  39. i=i/10;
  40. }
  41. while(j>0){
  42. count+=j%10;
  43. j=j/10;
  44. }
  45. return count;
  46. }
  47. }

329. 矩阵中的最长递增路径

说是记忆化dfs

  1. class Solution {
  2. public int longestIncreasingPath(int[][] matrix) {
  3. int m = matrix.length;
  4. if (m == 0) return 0;
  5. int n = matrix[0].length;
  6. int[][] note = new int[m][n];
  7. int max = 0;
  8. for (int i = 0; i < m; i++) {
  9. for (int j = 0; j < n; j++) {
  10. if (note[i][j] == 0) {
  11. dfs(i, j, matrix, note);
  12. }
  13. max = Math.max(max, note[i][j]);
  14. }
  15. }
  16. return max;
  17. }
  18. public int dfs(int i, int j, int[][] matrix, int[][] note) {
  19. if (note[i][j] != 0) {
  20. return note[i][j];
  21. }
  22. note[i][j]++;
  23. int[][] directions = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  24. for (int[] dir : directions) {
  25. int x = i + dir[0];
  26. int y = j + dir[1];
  27. if (x >= 0 && y >= 0 && x < matrix.length && y < matrix[0].length && matrix[x][y] > matrix[i][j]) {
  28. note[i][j] = Math.max(note[i][j], dfs(x, y, matrix, note) + 1);
  29. }
  30. }
  31. return note[i][j];
  32. }
  33. }