要解决这个问题,我们需要以环卫工人从小区到最近垃圾回收站行走的最短路径为基础,并累加所有小区到其最近垃圾回收站的距离。因为是一个网格,我们可以使用广度优先搜索(BFS)来有效地找到最短路径。
我们可以按照以下步骤实现这个解决方案:
-
初始化:
- 读取输入的矩阵行数
m 和列数 n。
- 构建和填充区域矩阵。
- 创建一个矩阵用于记录每个小区到最近垃圾回收站的最小距离,初始距离设为无穷大。
-
多源 BFS:
- 找到所有垃圾回收站 (
0 的位置),并将其作为初始点加入到 BFS 队列中。每个垃圾回收站的初始距离为 0。
- 使用 BFS 遍历网格,更新每个小区(
1 的位置)到最近垃圾回收站的距离。
-
计算最小距离和:
- 遍历所有小区的位置,如果其距离被更新过,累加距离以计算总最小距离。如果小区无法到达任何垃圾回收站(其距离未被更新),则不计入总和。
-
输出结果:
以下是上述思路的 Java 实现:
import java.util.*;
public class WasteCollection {
public static int minWasteCollectionDistance(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[][] distances = new int[m][n];
for (int[] row : distances) {
Arrays.fill(row, Integer.MAX_VALUE);
}
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
queue.add(new int[]{i, j});
distances[i][j] = 0;
}
}
}
int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int minDistanceSum = 0;
while (!queue.isEmpty()) {
int[] curr = queue.poll();
int x = curr[0];
int y = curr[1];
for (int[] dir : directions) {
int newX = x + dir[0];
int newY = y + dir[1];
if (newX >= 0 && newX < m && newY >= 0 && newY < n && grid[newX][newY] != -1 && distances[newX][newY] == Integer.MAX_VALUE) {
distances[newX][newY] = distances[x][y] + 1;
queue.add(new int[]{newX, newY});
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 && distances[i][j] != Integer.MAX_VALUE) {
minDistanceSum += distances[i][j];
}
}
}
return minDistanceSum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
scanner.nextLine(); // move to the next line
int[][] grid = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = scanner.nextInt();
}
}
System.out.println(minWasteCollectionDistance(grid));
}
}
在这个实现中,我们初始化一个 Queue,并将所有的垃圾处理站 (表示为 0) 添加进去。随后使用 BFS 更新每个位置到最近垃圾处理站的距离。如果某个小区 (表示为 1) 从未被更新,表明它无法到达任何垃圾处理站。最后累加所有可达小区的最短距离,获得结果。
#include <iostream>
#include <vector>
#include <algorithm>
struct Box {
int length, width, height;
// Ensure length > width for consistent rotation handling
Box(int l, int w, int h) {
if (l > w) {
length = l;
width = w;
} else {
length = w;
width = l;
}
height = h;
}
};
bool compare(const Box &a, const Box &b) {
return (a.length * a.width) > (b.length * b.width);
}
int maxStackHeight(std::vector<Box> &boxes) {
int n = boxes.size();
// Sort boxes based on base area
std::sort(boxes.begin(), boxes.end(), compare);
std::vector<int> maxHeight(n, 0);
for (int i = 0; i < n; ++i) {
maxHeight[i] = boxes[i].height;
}
// Build maximum stack height including boxes[i]
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (boxes[i].length < boxes[j].length &&
boxes[i].width < boxes[j].width) {
maxHeight[i] = std::max(maxHeight[i], maxHeight[j] + boxes[i].height);
}
}
}
// Find the maximum of all possible stack heights
int maxOverallHeight = *std::max_element(maxHeight.begin(), maxHeight.end());
return maxOverallHeight;
}
int main() {
int N;
std::cin >> N;
std::vector<Box> boxes;
for (int i = 0; i < N; ++i) {
int l, w, h;
std::cin >> l >> w >> h;
boxes.emplace_back(l, w, h);
boxes.emplace_back(l, h, w);
boxes.emplace_back(w, h, l);
}
std::cout << maxStackHeight(boxes) << std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
struct Box {
int length, width, height;
};
// Compare function for sorting boxes by base area (length * width)
bool compare(const Box &a, const Box &b) {
return (a.length * a.width) > (b.length * b.width);
}
int maxStackHeight(std::vector<Box> &boxes) {
int n = boxes.size();
std::vector<int> maxHeight(n);
// Initialize all heights to itself
for (int i = 0; i < n; i++) {
maxHeight[i] = boxes[i].height;
}
// Compute optimized maxHeight
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (boxes[i].length < boxes[j].length &&
boxes[i].width < boxes[j].width) {
maxHeight[i] = std::max(maxHeight[i], maxHeight[j] + boxes[i].height);
}
}
}
// Find maximum stack height
int maxOverallHeight = 0;
for (int i = 0; i < n; i++) {
maxOverallHeight = std::max(maxOverallHeight, maxHeight[i]);
}
return maxOverallHeight;
}
int main() {
int N;
std::cin >> N;
std::vector<Box> boxes;
// Read boxes and generate all possible rotations
for (int i = 0; i < N; i++) {
int l, w, h;
std::cin >> l >> w >> h;
// Add all rotations of the box
boxes.push_back({std::max(l, w), std::min(l, w), h});
boxes.push_back({std::max(l, h), std::min(l, h), w});
boxes.push_back({std::max(w, h), std::min(w, h), l});
}
// Sort the boxes by base area (length * width) descending
std::sort(boxes.begin(), boxes.end(), compare);
// Calculate maximum stack height
std::cout << maxStackHeight(boxes) << std::endl;
return 0;
}