#include <leetcode.h>
|
static int | minCost (vector< vector< int > > &grid) |
|
◆ minCost()
int leetcode::minimum_cost_to_make_at_least_one_valid_path_in_a_grid::Solution::minCost |
( |
vector< vector< int > > & |
grid | ) |
|
|
static |
在文件 leetcode.cpp 第 8142 行定义.
8143 const int m = grid.size();
8144 const int n = grid[0].size();
8145 vector g(m, vector<Node *>(n,
nullptr));
8146 for(
int i = 0; i < m; i++) {
8147 for(
int j = 0; j < n; j++) {
8148 g[i][j] =
new Node(i, j);
8151 for(
int i = 0; i < m; i++) {
8152 for(
int j = 0; j < n; j++) {
8153 pair<int, int> nexts[4] = {{i + 1, j}, {i - 1, j}, {i, j + 1}, {i, j - 1}};
8154 for(
auto [x, y]: nexts) {
8155 if(x >= 0 && x < m && y >= 0 && y < n) {
8156 g[i][j]->siblings.insert(make_pair(g[x][y], 1));
8161 switch(grid[i][j]) {
8175 if(x >= 0 && x < m && y >= 0 && y < n) {
8176 g[i][j]->siblings[g[x][y]] = 0;
8180 priority_queue<pair<int, Node *>, vector<pair<int, Node *>>, mycmp> pq;
8181 unordered_set<Node *> vis;
8182 pq.push({0, g[0][0]});
8183 while(!pq.empty()) {
8184 auto [cost, node] = pq.top();
8186 if(vis.contains(node)) {
8190 if(node->x == m - 1 && node->y == n - 1) {
8193 for(
auto [sibling, c]: node->siblings) {
8194 if(!vis.contains(sibling)) {
8195 pq.push({cost + c, sibling});
被这些函数引用 leetcode::minimum_cost_to_make_at_least_one_valid_path_in_a_grid::TEST().
该类的文档由以下文件生成: