problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::path_with_maximum_gold::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int get_sum (int current, int x, int y, int m, int n, vector< vector< int > > &grid, bool **occupied)
 
static int getMaximumGold (vector< vector< int > > &grid)
 

详细描述

在文件 leetcode.h892 行定义.

成员函数说明

◆ get_sum()

int leetcode::path_with_maximum_gold::Solution::get_sum ( int  current,
int  x,
int  y,
int  m,
int  n,
vector< vector< int > > &  grid,
bool **  occupied 
)
static

在文件 leetcode.cpp2231 行定义.

2231 {
2232 pair<int, int> nexts[] = {make_pair(x + 1, y), make_pair(x - 1, y), make_pair(x, y + 1), make_pair(x, y - 1)};
2233 int maximum = current;
2234 for(auto [next_x, next_y]: nexts) {
2235 if(0 <= next_x && next_x < m && 0 <= next_y && next_y < n && grid[next_x][next_y] != 0 && !occupied[next_x][next_y]) {
2236 auto *occupied_cpy = new bool *[m];
2237 for(int i = 0; i < m; i++) {
2238 occupied_cpy[i] = new bool[n];
2239 memcpy(occupied_cpy[i], occupied[i], n * sizeof(bool));
2240 }
2241 occupied_cpy[next_x][next_y] = true;
2242 maximum = max(maximum, get_sum(current + grid[next_x][next_y], next_x, next_y, m, n, grid, occupied_cpy));
2243 for(int i = 0; i < m; i++) {
2244 delete[] occupied_cpy[i];
2245 }
2246 delete[] occupied_cpy;
2247 }
2248 }
2249 return maximum;
2250 }
static int get_sum(int current, int x, int y, int m, int n, vector< vector< int > > &grid, bool **occupied)
Definition: leetcode.cpp:2231

引用了 get_sum().

被这些函数引用 get_sum() , 以及 getMaximumGold().

◆ getMaximumGold()

int leetcode::path_with_maximum_gold::Solution::getMaximumGold ( vector< vector< int > > &  grid)
static

在文件 leetcode.cpp2207 行定义.

2207 {
2208 const int m = grid.size();
2209 const int n = grid[0].size();
2210 int ans = 0;
2211 for(int i = 0; i < m; i++) {
2212 for(int j = 0; j < n; j++) {
2213 if(grid[i][j] != 0) {
2214 auto *occupied = new bool *[m];
2215 for(int k = 0; k < m; k++) {
2216 occupied[k] = new bool[n];
2217 memset(occupied[k], 0, n * sizeof(bool));
2218 }
2219 occupied[i][j] = true;
2220 ans = max(ans, get_sum(grid[i][j], i, j, m, n, grid, occupied));
2221 for(int k = 0; k < m; k++) {
2222 delete[] occupied[k];
2223 }
2224 delete[] occupied;
2225 }
2226 }
2227 }
2228 return ans;
2229 }

引用了 get_sum().

被这些函数引用 leetcode::path_with_maximum_gold::TEST().


该类的文档由以下文件生成: