import java.util.*;

class Solution {
public int cutOffTree(List<List<Integer>> forest) {
int m = forest.size();
int n = forest.get(0).size(); int[][][][] f = new int[m][n][m][n]; int max = m * n;
init(forest, m, n, f); // floyd(forest, n, f, max); List<int[]> list = new ArrayList<>();
list.add(new int[]{0,0,0});
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int val = forest.get(i).get(j);
if( val > 1){
list.add(new int[]{i,j,val});
}
}
}
Collections.sort(list, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[2] - o2[2];
}
}); int ans = 0;
for(int i=0;i<list.size()-1;i++){
int[] val = list.get(i);
int x = val[0];
int y = val[1];
int[] nextVal = list.get(i+1);
int tmp = dijkstra(forest,x,y,f,nextVal[0],nextVal[1]);
if(tmp == -1){
return -1;
}
ans += tmp;
} return ans;
} public int dijkstra(List<List<Integer>> forest,int x,int y ,int[][][][] f,int nextx,int nexty){
PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[2] - o2[2];
}
}); queue.offer(new int[]{x,y,0}); int m = forest.size();
int n = forest.get(0).size();
boolean[][] visited = new boolean[m][n];
visited[x][y] = true; while( !queue.isEmpty()) {
int[] node = queue.poll();
if( node[2] == Integer.MAX_VALUE){
continue;
}
if(node[0] == nextx &&node[1] == nexty){
return f[x][y][nextx][nexty];
}
int[][] dirs = new int[][]{
{0, 1}, {0, -1},
{-1, 0}, {1,0}
};
for (int i = 0; i < 4; i++) {
int newx = node[0] + dirs[i][0];
int newy = node[1] + dirs[i][1]; if (newx >= 0 && newx < m && newy >= 0 && newy < n && !visited[newx][newy] && forest.get(newx).get(newy)!=0) {
visited[newx][newy] = true;
int xk = node[0];
int yk = node[1];
f[x][y][newx][newy] = f[x][y][xk][yk]+1;
queue.add(new int[]{newx,newy,f[x][y][newx][newy]});
}
}
}
return -1;
} private void floyd(List<List<Integer>> forest, int n, int[][][][] f, int max) {
for (int k = 0; k < max; k++) {
int val = forest.get(k/ n).get(k% n);
if( val == 0){
continue;
}
for (int i = 0; i < max; i++) {
if (f[i / n][i % n][k / n][k % n] == Integer.MAX_VALUE) {
continue;
}
val = forest.get(i/ n).get(i% n);
if( val == 0){
continue;
}
for (int j = 0; j < max; j++) {
if (f[k / n][k % n][j / n][j % n] == Integer.MAX_VALUE) {
continue;
}
f[i / n][i % n][j / n][j % n] = Math.min(f[i / n][i % n][j / n][j % n], f[i / n][i % n][k / n][k % n] + f[k / n][k % n][j / n][j % n]);
}
}
}
} private void init(List<List<Integer>> forest, int m, int n, int[][][][] f) {
int[][] dirs = new int[][]{
{0, 1}, {0, -1},
{-1, 0}, {1,0}
};
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int ii = 0; ii < m; ii++) {
for (int jj = 0; jj < n; jj++) {
f[i][j][ii][jj] = Integer.MAX_VALUE;
if (i == ii && jj == j) {
f[i][j][i][j] = 0;
}
}
} }
} for (int x = 0; x < m; x++) {
for (int y = 0; y < n; y++) {
for (int d = 0; d < 4; d++) {
int newx = x + dirs[d][0];
int newy = y + dirs[d][1]; if (newx >= 0 && newx < m && newy >= 0 && newy < n) {
int v1 = forest.get(x).get(y);
int v2 = forest.get(newx).get(newy); if (v1 == 0 || v2 == 0) {
f[x][y][newx][newy] = Integer.MAX_VALUE;
f[newx][newy][x][y] = Integer.MAX_VALUE;
} else {
f[x][y][newx][newy] = 1;
f[newx][newy][x][y] = 1;
}
}
}
}
}
} }

[675. 为高尔夫比赛砍树] dijkstra算法的更多相关文章

  1. Leetcode 675.为高尔夫比赛砍树

    为高尔夫比赛砍树 你被请来给一个要举办高尔夫比赛的树林砍树. 树林由一个非负的二维数组表示, 在这个数组中: 0 表示障碍,无法触碰到. 1 表示可以行走的地面. 比1大的数 表示一颗允许走过的树的高 ...

  2. [Swift]LeetCode675. 为高尔夫比赛砍树 | Cut Off Trees for Golf Event

    You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...

  3. [LeetCode] 675. Cut Off Trees for Golf Event 为高尔夫赛事砍树

    You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...

  4. [LeetCode] Cut Off Trees for Golf Event 为高尔夫赛事砍树

    You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...

  5. 经典树与图论(最小生成树、哈夫曼树、最短路径问题---Dijkstra算法)

    参考网址: https://www.jianshu.com/p/cb5af6b5096d 算法导论--最小生成树 最小生成树:在连通网的所有生成树中,所有边的代价和最小的生成树,称为最小生成树. im ...

  6. Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

    /* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...

  7. Dijkstra算法(二)之 C++详解

    本章是迪杰斯特拉算法的C++实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法图解 3. 迪杰斯特拉算法的代码说明 4. 迪杰斯特拉算法的源码 转载请注明出处:http://www.cnbl ...

  8. 【Python排序搜索基本算法】之Dijkstra算法

    Dijkstra算法和前一篇的Prim算法非常像,区别就在于Dijkstra算法向最短路径树(SPT)中添加顶点的时候,是按照ta与源点的距离顺序进行的.OSPF动态路由协议就是用的Dijkstra算 ...

  9. Cocos2d-x 地图步行实现1:图论Dijkstra算法

    下一节<Cocos2d-x 地图行走的实现2:SPFA算法>: http://blog.csdn.net/stevenkylelee/article/details/38440663 本文 ...

  10. 最短路径算法——Dijkstra算法

    在路由选择算法中都要用到求最短路径算法.最出名的求最短路径算法有两个,即Bellman-Ford算法和Dijkstra算法.这两种算法的思路不同,但得出的结果是相同的. 下面只介绍Dijkstra算法 ...

随机推荐

  1. CH32V208蓝牙从机sleep模式下功耗测试

    本测试基于CH32V208W的开发板:蓝牙从机模式:使用程序BLE_UART 在进行功耗测试的时候尽量去除额外耗电器件,将开发板上的VDD于VIO相连接,测功耗时直接给VDD供电. 将会对500ms, ...

  2. The Missing Semester - 第五讲 学习笔记

    第五讲 命令行环境 课程视频地址:https://www.bilibili.com/video/BV1Dy4y1a7BW 课程讲义地址:https://missing-semester-cn.gith ...

  3. NC17872 CSL的校园卡

    题目链接 题目 题目描述 今天是阳光明媚,晴空万里的一天,CSL早早就高兴地起床走出寝室到校园里转悠. 但是,等到他回来的时候,发现他的校园卡不见了,于是他需要走遍校园寻找它的校园卡.CSL想要尽快地 ...

  4. Wireguard笔记(二) 命令行操作

    目录 Wireguard笔记(一) 节点安装配置和参数说明 Wireguard笔记(二) 命令行操作 Wireguard笔记(三) lan-to-lan子网穿透和多网段并存 命令行操作 创建wg0网卡 ...

  5. centos 安装nacos 并以后台服务形式启动

    一.下载解压nacos tar -xvf nacos-server-1.2.0.tar.gz 二.持久化配置(mysql) 修改nacos/conf/application.properties文件, ...

  6. Java 23种设计模式的分类和使用场景

    听说过GoF吧? GoF是设计模式的经典名著Design Patterns: Elements of Reusable Object-Oriented Software(中译本名为<设计模式-- ...

  7. GROW模型:世界上最常用的教练模型(**案例及全套表单)

    http://www.360doc.com/content/21/0812/18/76566468_990752480.shtml <高绩效教练>一书的核心方法就是GROW模型. GROW ...

  8. window上使用Putty通过ssh远程连接并通过Xming实现X11图形界面功能

    # 0.先确认远程服务器的ssh配置 >>> grep X11 /etc/ssh/sshd_config X11Forwarding yes #X11DisplayOffset 10 ...

  9. ASP.NET 读取FTP文件流

    参考资料 ASP.NET 上传文件到共享文件夹 工具类代码 /// <summary> /// 读取ftp文件流 /// </summary> /// <param na ...

  10. ABP模块的测试项目从默认的Microsoft SQL Server替换成MySQL

    1.替换项目引用 2.重新生成解决方案 3.删除Migrations 4.模块的引用 替换成:AbpEntityFrameworkCoreMySQLModule 5.命名空间 替换成:Volo.Abp ...