📖 Content of this article: leetcode daily question 1219 Gold miner DFS deep search violence AC on the fifth day of the first month, I wish you a wide range of money~
📑 Article column: leetcode daily question "punch in daily"
📆 Last updated: February 4, 2022 leetcode daily question 1725 The number of rectangles that can form the largest square is stored in the hash table for greedy traversal optimization~
🙊 Personal profile: a Junior Program ape who is studying in a two-year college. In line with paying attention to foundation, clock in algorithm and sharing technology, as a blogger who summarizes personal experience, although he may be lazy sometimes, he will stick to it. If you like blog very much, it is suggested to look at the following line ~ (crazy hint QwQ)
🌇 give the thumbs-up 👍 Collection ⭐ Leaving a message. 📝 One key three connection care program ape, start with you and me
🙊 Write in front 🙊
Welcome the God of wealth on the fifth day of the lunar new year, and the gold miners are safe~
subject
You want to develop a gold mine. Geological surveyors have identified the distribution of resources in the gold mine and marked it with a grid of size m * n. The integer in each cell represents the amount of gold in this cell; If the cell is empty, it is 0.
In order to maximize profits, miners need to mine gold according to the following rules:
Every time a miner enters a cell, he collects all the gold in that cell.
Miners can walk up, down, left and right from their current position at a time.
Each cell can only be mined (entered) once.
Do not mine (enter) cells with a gold number of 0.
Miners can start from any cell with gold in the grid or stop.
Examples
Example 1:
Input: k = 7 Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] One route to collect the most gold is: 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] One route to collect the most gold is: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Tips
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There is gold in up to 25 cells.
📝 thinking 📝
This question should be violent AC. for each starting position, look for the profit value from the current point to other points, but there are many results. If the result reaches the current point and the result value reaches another point is not the largest, it needs to be backtracked. However, according to the principle of violent AC, let's have A try.
Xiao Fu also thought about how to solve it with bfs, but the efficiency is really a little low = - = fortunately, he tried. The efficiency of dfs is a little higher. Personally, I think the optimal solution should be direct backtracking record processing.
⭐ code implementation ⭐
Violence Act
class Solution { //Record the horizontal and vertical coordinates of up, down, left and right mining, corresponding to up, down, left and right one by one int dx[] = new int[]{0,0,-1,1}; int dy[] = new int[]{1,-1,0,0}; public int getMaximumGold(int[][] grid) { if (grid.length == 0)return 0; int m = grid.length; int n = grid[0].length; //Maintain maxVal to record the maximum mineral value gain int maxVal = 0; //Traverse each point for deep search for (int i = 0;i< m;i++){ for (int j = 0 ; j< n;j++){ //The value of each point is compared to obtain the maximum benefit value maxVal = Math.max(maxVal,dfs(grid,i,j)); } } return maxVal; } public int dfs(int[][] grid,int i ,int j){ //If it is out of bounds or the current pit has no value, returning directly is also the termination condition of recursion if (i < 0|| i>= grid.length|| j<0 || j>= grid[0].length || grid[i][j] == 0)return 0; //Record the value of the current pit int curVal = grid[i][j]; //Yes, the current pit is gone grid[i][j] = 0; //Record the maximum mineral value gain of the search int maxVal = 0; //Start to search the needle for the up, down, left and right of each point Search recursively to find the most appropriate path for (int k = 0 ; k< 4;k++){ int newX = i + dx[k]; int newY = j + dy[k]; maxVal = Math.max(maxVal,dfs(grid,newX,newY)); } //Assign the value of the original record back to facilitate the traversal of the next I and j nodes grid[i][j] = curVal; //Returns the maximum return value that can be searched from the current node return grid[i][j] + maxVal; } }
Operation results
DFS
🙊 Write at the end 🙊
2022-2-5 Xiaofu clocked in today~
Today is the fifth day of the lunar new year. I wish you a wide range of financial resources and a happy face~
Beautiful sunrise, beautiful mountains and rivers
Because of your presence