2023华为OD机试 (B卷)|200分 寻找最大价值的矿堆(Java JavaScript C++ Python)

题目描述

给你一个由'0'(空地)、'1'(银矿)、'2'(金矿)组成的地图,矿堆只能由上下左右相邻的金矿或银矿连接形成。超出地图范围可以认为是空地。
假设银矿价值1,金矿价值2,请你找出地图中最大价值的矿堆并输出该矿堆的价值。

输入描述

地图元素信息如下:

22220
00000
00000
11111

  • 地图范围最大 300*300
  • 0 <=地图元素 <= 2
输出描述

矿堆的最大价值

用例1

输入:

22220
00000
00000
01111

输出:
8

用例2

输入:

20000
00020
00000
00111

输出
3

Java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    // 地图矩阵
    static int[][] map;

    // 上下左右,四个方向的偏移量
    static int[][] offsets = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 读入地图信息
        ArrayList<String> lines = new ArrayList<>();
        while (sc.hasNextLine()) {
            lines.add(sc.nextLine());
        }

        // 构建地图矩阵
        int rows = lines.size();
        int cols = lines.get(0).length();
        map = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            String line = lines.get(i);
            for (int j = 0; j < cols; j++) {
                map[i][j] = line.charAt(j) - '0';
            }
        }

        // 记录最大矿堆价值
        int maxVal = 0;

        // 遍历地图矩阵
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                // 如果点(i,j)没有被访问过,且点(i,j)上有矿,则进入深搜
                if (map[i][j] > 0) {
                    LinkedList<int[]> stack = new LinkedList<>();
                    stack.add(new int[]{i, j});

                    int sum = 0;

                    while (!stack.isEmpty()) {
                        int[] pos = stack.removeLast();
                        int x = pos[0], y = pos[1];

                        sum += map[x][y];
                        map[x][y] = 0;

                        // 遍历四个方向
                        for (int[] offset : offsets) {
                            int newX = x + offset[0];
                            int newY = y + offset[1];

                            // 如果新位置在地图范围内,且有矿,则加入栈中
                            if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && map[newX][newY] > 0) {
                                stack.add(new int[]{newX, newY});
                            }
                        }
                    }

                    // 更新最大矿堆价值
                    maxVal = Math.max(maxVal, sum);
                }
            }
        }

        System.out.println(maxVal);
    }
}


JavaScript
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

// 地图矩阵
let map;

// 上下左右,四个方向的偏移量
const offsets = [[-1, 0], [1, 0], [0, -1], [0, 1]];
let lines = [];
rl.on('line', (line) => {
  lines.push(line);
}).on('close', () => {
 // 构建地图矩阵
    const rows = lines.length;
    const cols = lines[0].length;
    map = new Array(rows);
    for (let i = 0; i < rows; i++) {
      map[i] = new Array(cols);
      const str = lines[i];
      for (let j = 0; j < cols; j++) {
        map[i][j] = parseInt(str.charAt(j));
      }
    }

    // 记录最大矿堆价值
    let maxVal = 0;

    // 遍历地图矩阵
    for (let i = 0; i < rows; i++) {
      for (let j = 0; j < cols; j++) {
        // 如果点(i,j)没有被访问过,且点(i,j)上有矿,则进入深搜
        if (map[i][j] > 0) {
          const stack = [];
          stack.push([i, j]);

          let sum = 0;

          while (stack.length > 0) {
            const pos = stack.pop();
            const x = pos[0], y = pos[1];

            sum += map[x][y];
            map[x][y] = 0;

            // 遍历四个方向
            for (const offset of offsets) {
              const newX = x + offset[0];
              const newY = y + offset[1];

              // 如果新位置在地图范围内,且有矿,则加入栈中
              if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && map[newX][newY] > 0) {
                stack.push([newX, newY]);
              }
            }
          }

          // 更新最大矿堆价值
          maxVal = Math.max(maxVal, sum);
        }
      }
    }

    console.log(maxVal);
    rl.close();
})


C++
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

// 地图矩阵
int map[100][100];

// 上下左右,四个方向的偏移量
int offsets[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int main() {
    // 读入地图信息
    vector<string> lines;
    string line;
    while (getline(cin, line)) {
        lines.push_back(line);
    }

    // 构建地图矩阵
    int rows = lines.size();
    int cols = lines[0].size();
    for (int i = 0; i < rows; i++) {
        line = lines[i];
        for (int j = 0; j < cols; j++) {
            map[i][j] = line[j] - '0';
        }
    }

    // 记录最大矿堆价值
    int maxVal = 0;

    // 遍历地图矩阵
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            // 如果点(i,j)没有被访问过,且点(i,j)上有矿,则进入深搜
            if (map[i][j] > 0) {
                queue<pair<int, int>> q;
                q.push(make_pair(i, j));

                int sum = 0;

                while (!q.empty()) {
                    pair<int, int> pos = q.front();
                    q.pop();
                    int x = pos.first, y = pos.second;

                    sum += map[x][y];
                    map[x][y] = 0;

                    // 遍历四个方向
                    for (auto offset : offsets) {
                        int newX = x + offset[0];
                        int newY = y + offset[1];

                        // 如果新位置在地图范围内,且有矿,则加入队列中
                        if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && map[newX][newY] > 0) {
                            q.push(make_pair(newX, newY));
                        }
                    }
                }

                // 更新最大矿堆价值
                maxVal = max(maxVal, sum);
            }
        }
    }

    cout << maxVal << endl;

    return 0;
}


Python
from collections import deque

# 地图矩阵
map = []

# 上下左右,四个方向的偏移量
offsets = [(-1, 0), (1, 0), (0, -1), (0, 1)]

# 读入地图信息
lines = []
while True:
    try:
        line = input()
        lines.append(line)
    except:
        break

# 构建地图矩阵
rows = len(lines)
cols = len(lines[0])
map = [[0 for j in range(cols)] for i in range(rows)]
for i in range(rows):
    line = lines[i]
    for j in range(cols):
        map[i][j] = int(line[j])

# 记录最大矿堆价值
maxVal = 0

# 遍历地图矩阵
for i in range(rows):
    for j in range(cols):
        # 如果点(i,j)没有被访问过,且点(i,j)上有矿,则进入深搜
        if map[i][j] > 0:
            stack = deque()
            stack.append((i, j))

            sum = 0

            while stack:
                pos = stack.pop()
                x, y = pos

                sum += map[x][y]
                map[x][y] = 0

                # 遍历四个方向
                for offset in offsets:
                    newX = x + offset[0]
                    newY = y + offset[1]

                    # 如果新位置在地图范围内,且有矿,则加入栈中
                    if newX >= 0 and newX < rows and newY >= 0 and newY < cols and map[newX][newY] > 0:
                        stack.append((newX, newY))

            # 更新最大矿堆价值
            maxVal = max(maxVal, sum)

print(maxVal)


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值