簡體   English   中英

初始化 std::memset

[英]Initializing std::memset

我正在使用std::memset來解決此處復制的LeetCode 問題 目前, map_cherries已初始化為:

map_cherries[70][70][70] = {}
...
std::memset(map_cherries, -1, sizeof(map_cherries));

有沒有辦法同時設置map_cherries的尺寸和值,或者可能有更好/替代的方法來做到這一點? 謝謝!

問題

給定一個代表櫻桃田的rows x cols矩陣網格。 網格中的每個單元格代表您可以收集的櫻桃數量。

您有兩個可以為您收集櫻桃的機器人,Robot #1 位於網格的左上角(0,0) ,Robot #2 位於網格的右上角(0, cols-1) .

按照以下規則返回使用兩個機器人收集的最大櫻桃數量:

  • 從單元格(i,j) ,機器人可以移動到單元格(i+1, j-1)(i+1, j)(i+1, j+1)
  • 當任何機器人經過一個單元格時,它會撿起所有的櫻桃,這個單元格就變成了一個空單元格(0)。
  • 當兩個機器人都呆在同一個牢房里時,只有一個機器人拿走櫻桃。
  • 兩個機器人在任何時候都不能移出網格。
  • 兩個機器人都應該到達網格的最后一行。

示例 1:

在此處輸入圖像描述

輸入: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output:24

說明:機器人#1 和#2 的路徑分別用綠色和藍色表示。
機器人 #1 采取的櫻桃, (3 + 2 + 5 + 2) = 12

機器人 #2 采摘的櫻桃, (1 + 5 + 5 + 1) = 12 櫻桃總數: 12 + 12 = 24

示例 2:

在此處輸入圖像描述

輸入: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]

Output:28

說明:機器人#1 和#2 的路徑分別用綠色和藍色表示。
機器人 #1 采取的櫻桃, (1 + 9 + 5 + 2) = 17

機器人 #2 采摘的櫻桃, (1 + 3 + 4 + 3) = 11

櫻桃總數: 17 + 11 = 28

約束:

  • rows == grid.length
  • cols == grid[i].length
  • 2 <= rows, cols <= 70
  • 0 <= grid[i][j] <= 100

試圖

#include <cstdint>
#include <vector>
#include <cstring>

struct Solution {
    int map_cherries[70][70][70] = {};

    int cherryPickup(std::vector<std::vector<int>>& grid) {
        std::memset(map_cherries, -1, sizeof(map_cherries));
        const std::size_t row_length = grid.size();
        const std::size_t col_length = grid[0].size();
        return depth_first_search(grid, row_length, col_length, 0, 0, col_length - 1);
    }

private:
    int depth_first_search(
        std::vector<std::vector<int>>& grid,
        const std::size_t row_length,
        const std::size_t col_length,
        int row,
        int left_robot,
        int right_robot
    ) {
        if (row == row_length) {
            return 0;
        }

        if (map_cherries[row][left_robot][right_robot] != -1) {
            return map_cherries[row][left_robot][right_robot];
        }

        int max_cherries = 0;

        for (int left = -1; left < 2; left++) {
            for (int right = -1; right < 2; right++) {
                const int curr_left_robot = left_robot + left;
                const int curr_right_robot = right_robot + right;

                if (curr_left_robot > -1 and curr_left_robot < col_length and curr_right_robot > -1 and curr_right_robot < col_length) {
                    max_cherries = std::max(max_cherries, depth_first_search(
                                                grid,
                                                row_length,
                                                col_length,
                                                row + 1,
                                                curr_left_robot,
                                                curr_right_robot
                                            ));
                }
            }
        }

        int total_cherries = grid[row][left_robot];

        if (left_robot != right_robot) {
            total_cherries += grid[row][right_robot];
        }

        total_cherries += max_cherries;
        return map_cherries[row][left_robot][right_robot] = total_cherries;
    }
};

輸入

[[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
[[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]]
[[1,10,0,3,86,40],[0,0,0,3,86,40],[0,0,3,3,86,40],[9,0,3,3,86,40], [1,0,40,3,86,40],[0,22,0,3,86,40],[99,0,3,3,86,40],[9,0,3,3,86,40]]

輸出

24
22
819

參考

一種方法是這樣的:

auto map_cherries = std::vector(70, std::vector(70, std::vector(70, -1)));

僅適用於從 c++17 開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM