当前位置:网站首页>Force deduction solution summary 675- cutting trees for golf competition
Force deduction solution summary 675- cutting trees for golf competition
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
You're invited to cut trees for a forest that's going to hold a golf game . The forest consists of a m x n The matrix representation of , In this matrix :
0 Indicates an obstacle , Can't touch
1 Represents the ground , Can walk
Than 1 Large number Represents a cell with a tree , Can walk , The value represents the height of the tree
Each step , You can go up 、 Next 、 Left 、 Move one unit in one of the four directions to the right , If there is a tree where you stand , Then you can decide whether to cut it down .
You need to cut down all the trees from low to high according to the height of the tree , Every time I cut a tree , The value of this cell becomes 1( It becomes the ground ).
You will (0, 0) Start work at , Return to the minimum number of steps you need to take to cut down all trees . If you can't cut all the trees , return -1 .
What can be guaranteed is , No two trees are the same height , And you need to cut down at least one tree .
Example 1:
Input :forest = [[1,2,3],[0,0,4],[7,6,5]]
Output :6
explain : Follow the path above , You can use it. 6 Step , Cut down the trees from the lowest to the highest .
Example 2:
Input :forest = [[1,2,3],[0,0,0],[7,6,5]]
Output :-1
explain : Because the middle row is blocked by obstacles , Unable to access the tree in the bottom row .
Example 3:
Input :forest = [[2,3,4],[0,0,5],[8,7,6]]
Output :6
explain : You can follow the example 1 The same path to cut down all the trees .
(0,0) The trees of the location , You can cut it directly , Don't count the steps .
Tips :
m == forest.length
n == forest[i].length
1 <= m, n <= 50
0 <= forest[i][j] <= 109
source : Power button (LeetCode)
link :https://leetcode.cn/problems/cut-off-trees-for-golf-event
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
Refer to the official explanation .
Because it needs to be cut according to the height , So it is actually to find the shortest distance between two adjacent heights .
The shortest distance between the two , We just use DP To solve the problem .
Code :
int[][] dirs = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public int cutOffTree(List<List<Integer>> forest) {
List<int[]> trees = new ArrayList<int[]>();
int row = forest.size();
int col = forest.get(0).size();
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (forest.get(i).get(j) > 1) {
trees.add(new int[]{i, j});
}
}
}
Collections.sort(trees, (a, b) -> forest.get(a[0]).get(a[1]) - forest.get(b[0]).get(b[1]));
int cx = 0;
int cy = 0;
int ans = 0;
for (int i = 0; i < trees.size(); ++i) {
int steps = bfs(forest, cx, cy, trees.get(i)[0], trees.get(i)[1]);
if (steps == -1) {
return -1;
}
ans += steps;
cx = trees.get(i)[0];
cy = trees.get(i)[1];
}
return ans;
}
public int bfs(List<List<Integer>> forest, int sx, int sy, int tx, int ty) {
if (sx == tx && sy == ty) {
return 0;
}
int row = forest.size();
int col = forest.get(0).size();
int step = 0;
Queue<int[]> queue = new ArrayDeque<int[]>();
boolean[][] visited = new boolean[row][col];
queue.offer(new int[]{sx, sy});
visited[sx][sy] = true;
while (!queue.isEmpty()) {
step++;
int sz = queue.size();
for (int i = 0; i < sz; ++i) {
int[] cell = queue.poll();
int cx = cell[0], cy = cell[1];
for (int j = 0; j < 4; ++j) {
int nx = cx + dirs[j][0];
int ny = cy + dirs[j][1];
if (nx >= 0 && nx < row && ny >= 0 && ny < col) {
if (!visited[nx][ny] && forest.get(nx).get(ny) > 0) {
if (nx == tx && ny == ty) {
return step;
}
queue.offer(new int[]{nx, ny});
visited[nx][ny] = true;
}
}
}
}
}
return -1;
}边栏推荐
- 力扣解法汇总933-最近的请求次数
- Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
- Force deduction solution summary 824 goat Latin
- [adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology
- Leetcode 45 jump game II
- ozzanimation-基於sse的動作系統
- PHP builds a high-performance API architecture based on sw-x framework (III)
- 2022西式面点师(技师)复训题库及在线模拟考试
- What are the advantages of adaptive search advertising?
- 为什么我们要使用谷歌搜索广告?
猜你喜欢

通用树形结构的迭代与组合模式实现方案

BaseDexClassLoader那些事

DDD的分层架构

Operating mechanism of Google ads bidding

Basedexclassloader

SQL calculates KS, AUC, IV, psi and other risk control model indicators

el-upload上传文件

C language programming classic games - minesweeping

Explore performance optimization! Performance improvement from 2 months to 4 hours!

Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
随机推荐
Force deduction solution summary 398 random number index
RPA introduction
通过搜索广告附加信息让广告更具相关性
serialization and deserialization
Oracle 11g graphic download installation tutorial (step by step)
Modification of system module information of PHP security development 12 blog system
Three main factors determining advertising quality
Almost all schools will ask for the second round exam! Come in and recite the answer!
How to locate keywords to make advertising accurate.
What are the advantages of adaptive search advertising?
Linux(CentOS6)安装MySQL5.5版本数据库
Swiftyjson parsing local JSON files
PHP development 09 article module deletion and article classification writing
Force deduction solution summary 388- longest absolute path of file
ozzanimation-基于sse的动作系统
Force deduction solution summary 883- projected area of 3D shape
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology
leetcode:6. Zigzag transformation
android html5页面加载缓存优化
Google 搜索广告系列设置前有哪些准备工作?