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. ASP.NET Core分布式项目实战(第三方ClientCredential模式调用)--学习笔记

    任务10:第三方ClientCredential模式调用 创建一个控制台程序 dotnet new console --name ThirdPartyDemo 添加 Nuget 包:IdentityM ...

  2. NC24727 [USACO 2010 Feb G]Slowing down

    题目链接 题目 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered ...

  3. NC19975 [HAOI2008]移动玩具

    题目链接 题目 题目描述 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移 ...

  4. SATA学习笔记——OOB信号

    一.SATA物理层概述 说OOB之前,首先得了解一下SATA结构以及物理层的含义. SATA主要包括:应用层(Application Layer), 传输层(Transport Layer),链路层( ...

  5. 对称加密算法汇总:AES DES 3DES SM4 java 实现入门

    密码的世界 如果你是黑帮老大,平时和手下沟通,如何保证自己的信息安全呢? 在神探夏洛克的第一季中,就讲述了一个如何侦破黑帮的加密交流的故事. 这种密码利用的是密码字典. 密码本身可以是一本书,比如常见 ...

  6. IPFS 添加和管理文件

    IPFS的文件有不同的模式 默认模式 默认模式下, 文件会被解析并存入blocks, 同时文件的结构被存入filestore, 因为IPFS是按内容寻址的文件系统, 在添加时最外层的目录名或文件名信息 ...

  7. Js中Array对象

    Js中Array对象 JavaScript的Array对象是用于构造数组的全局对象,数组是类似于列表的高阶对象. 描述 在JavaScript中通常可以使用Array构造器与字面量的方式创建数组. c ...

  8. Oracle 12c中增强的PL/SQL功能

    英文链接:http://www.oracle.com/technetwork/issue-archive/2013/13-sep/o53plsql-1999801.html Oracle 12c增强了 ...

  9. kubernetes(k8s)大白学习01-kubernetes是什么?有什么用?

    kubernetes(k8s)大白基础学习-kubernetes是什么? 一.认识 Docker Docker 是什么 先来看看 Docker 的图标: 一条鲸鱼背上驮着四方形块的物品,就像一条海运船 ...

  10. Redis搭建Sentinel实验环境

    环境准备 在物理机上启动3台虚拟机,IP地址分别为:192.168.56.4,192.168.56.5,192.168.56.6. 1.确保3台虚拟机的网络是相互联通的. 2.确保已经在3台虚拟机上安 ...