
public class GetResourcesTime {
public static void main(String[] args) {
int startX = 2;
int startY = 3;
int[][] resources = {{1, 5}, {3, 2}, {4, 3}};
int n = resources.length;
int[] distances = new int[n];
for (int i = 0; i < n; i++) {
distances[i] = Math.abs(resources[i][0]-startX) + Math.abs(resources[i][1]-startY);
}
int totalTime = 0;
int currX = startX;
int currY = startY;
while (true) {
int minDist = Integer.MAX_VALUE;
int minIndex = -1;
for (int i = 0; i < n; i++) {
if (distances[i] >= 0 && distances[i] < minDist) {
minDist = distances[i];
minIndex = i;
}
}
if (minIndex == -1) {
break;
}
totalTime += Math.abs(resources[minIndex][0]-currX) + Math.abs(resources[minIndex][1]-currY);
currX = resources[minIndex][0];
currY = resources[minIndex][1];
distances[minIndex] = -1;
}
System.out.println("取得所有资源的最少时间为:" + totalTime);
}
}
```java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int startX = 0, startY = 0;
int[][] resources = {{1, 2}, {3, 4}, {5, 6}};
int totalTime = 0;
int curX = startX, curY = startY;
Arrays.sort(resources, (a, b) -> (Math.abs(a[0] - curX) + Math.abs(a[1] - curY)) - (Math.abs(b[0] - curX) + Math.abs(b[1] - curY)));
for (int[] resource : resources) {
totalTime += Math.abs(resource[0] - curX) + Math.abs(resource[1] - curY);
curX = resource[0];
curY = resource[1];
}
System.out.println(totalTime);
}
}