34.Path in matrix
Title:
Given an m x n two-dimensional character grid board and a string word word. If word exists in the grid, return true; Otherwise, false is returned. Words must be formed alphabetically by letters in adjacent cells, where "adjacent" cells are those horizontally or vertically adjacent. Letters in the same cell cannot be reused.
Solution:
DFS depth first search
- Traverse the two-dimensional array board to find the first letter of word, and take this as the starting point for depth first search;
- Note: in a double loop, you cannot use return dfs(board, chars, i, j, 0) instead of if (DFS (board, chars, I, J, 0)) to return true; Because there may be more than one first two letter entry in the DFS array.
- Recursive function dfs
- If the subscript is out of bounds or the current element board [i] [j] is not equal to word[k], false is returned;
- If the function continues to execute, it means that the current element board [i] [j] is equal to word[k]. At this time, if k = = chars Length - 1, that is, the current element is equal to the last letter of word, that is, the traversal ends and returns true;
- If it is the middle letter of word, use board[a][b] = ' board [a] [b] = '\ 0'' for this element; Statement, and continue to recursively traverse its upper, lower, left and right elements;
- Be sure to use board[a][b] = chars[k] for the covered elements after the recursion is completed; For restoration, ensure that the two-dimensional array is in the initial state during the next DFS.
public boolean exist(char[][] board, String word) { char[] chars = word.toCharArray(); for (int i = 0; i < board.length; i++){ for (int j = 0; j < board[0].length; j++){ if(dfs(board, chars, i, j, 0)) return true; } } return false; } boolean dfs(char[][] board, char[] chars, int a, int b, int k){ if (a < 0 || b < 0 || a >= board.length || b >= board[0].length || board[a][b] != chars[k]) return false; if (k == chars.length - 1) return true; board[a][b] = '\0'; boolean res = dfs(board, chars, a, b - 1,k+1) || dfs(board,chars,a + 1, b,k+1) || dfs(board, chars, a - 1,b, k+1) || dfs(board, chars, a, b + 1,k+1); board[a][b] = chars[k]; return res; }
35.Range of motion of robot
Title:
There is a square with m rows and N columns on the ground, from coordinates [0,0] to coordinates [m-1,n-1]. A robot starts to move from the grid of coordinates [0,0]. It can move left, right, up and down one grid at a time (it can't move outside the grid), and it can't enter the grid where the sum of digits of row coordinates and column coordinates is greater than k. For example, when k is 18, the robot can enter the grid [35, 37], because 3 + 5 + 3 + 7 = 18. But it cannot enter the grid [35, 38], because 3 + 5 + 3 + 8 = 19. How many grids can the robot reach?
Solution:
DFS depth first traversal
- boolean[][] visited is used to indicate whether the location is accessed. Initialization is all false and set to true after being accessed;
- dfs end condition of recursive function
- i and j cross the border;
- The digit sum si + sj of i and j is greater than k;
- visited[i][j] is true, that is, the location is accessed.
- Recursion end condition not reached
- Set the element at this position of the visited array to true;
- Call the recursive function down and right, and pay attention to the calculation of the sum of digits;
- Return 1 + DFS (down) + DFS (right);
- Calculation of the sum of digits
- If (x+1)%10 == 0, the sum of digits is - 8, such as x = 19. After moving down or right, it becomes 20, and the sum of digits changes from 10 to 2;
- If (x+1)%10= 0, then the sum of digits + 1, such as x = 12, changes to 13 after moving down or right, and the sum of digits changes from 3 to 4.
class Solution { int row,column,k; boolean[][] visited; public int movingCount(int m, int n, int k) { row = m; column = n; this.k = k; this.visited = new boolean[m][n]; return dfs(0,0,0,0); } int dfs (int i, int j, int si, int sj){ if (i > row - 1 || j > column - 1 || si + sj > k || visited[i][j]) return 0; visited[i][j] = true; return 1 + dfs(i + 1, j,(i + 1) % 10 == 0 ? si - 8 : si + 1, sj) + dfs(i,j + 1, si, (j + 1) % 10 == 0 ? sj - 8: sj + 1); } }
Once again, he was secretly sad about the fate of others.