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

#include <leetcode.h>

静态 Public 成员函数

static int shortestPathAllKeys (vector< string > &grid)
 

详细描述

在文件 leetcode.h3247 行定义.

成员函数说明

◆ shortestPathAllKeys()

int leetcode::shortest_path_to_get_all_keys::Solution::shortestPathAllKeys ( vector< string > &  grid)
static

在文件 leetcode.cpp9241 行定义.

9241 {
9242 priority_queue<frame> pq;
9243 int m = grid.size();
9244 int n = grid[0].size();
9245 int start_x = 0;
9246 int start_y = 0;
9247 int lock_cnt = 0;
9248 unordered_map<unsigned, vector<vector<int>>> min_step;
9249 for(int i = 0; i < m; i++) {
9250 for(int j = 0; j < n; j++) {
9251 if(grid[i][j] == START) {
9252 start_x = i;
9253 start_y = j;
9254 } else if(isupper(grid[i][j])) {
9255 lock_cnt++;
9256 }
9257 }
9258 }
9259 frame start(start_x, start_y, lock_cnt);
9260 pq.push(start);
9261 while(!pq.empty()) {
9262 frame f = pq.top();
9263 pq.pop();
9264 if(f.keys.size() == lock_cnt) {
9265 return f.step;
9266 }
9267 if(!min_step.contains(f.get_mask())) {
9268 min_step[f.get_mask()] = vector(m, vector(n, INT_MAX));
9269 }
9270 if(min_step[f.get_mask()][f.x][f.y] <= f.step) {
9271 continue;
9272 }
9273 min_step[f.get_mask()][f.x][f.y] = f.step;
9274 const pair<int, int> nexts[4] = {{f.x + 1, f.y}, {f.x - 1, f.y}, {f.x, f.y + 1}, {f.x, f.y - 1}};
9275 for(const auto &[n_x, n_y]: nexts) {
9276 if(n_x >= 0 && n_x < m && n_y >= 0 && n_y < n && grid[n_x][n_y] != WALL) {
9277 if(islower(grid[n_x][n_y])) {
9278 frame next = f;
9279 next.x = n_x;
9280 next.y = n_y;
9281 next.step++;
9282 next.keys.insert(grid[n_x][n_y]);
9283 if(!min_step.contains(next.get_mask())) {
9284 min_step[next.get_mask()] = vector(m, vector(n, INT_MAX));
9285 }
9286 if(min_step[next.get_mask()][n_x][n_y] > next.step) {
9287 pq.push(next);
9288 }
9289 } else if(isupper(grid[n_x][n_y])) {
9290 if(f.keys.contains(static_cast<char>(tolower(grid[n_x][n_y])))) {
9291 frame next = f;
9292 next.x = n_x;
9293 next.y = n_y;
9294 next.step++;
9295 if(!min_step.contains(next.get_mask())) {
9296 min_step[next.get_mask()] = vector(m, vector(n, INT_MAX));
9297 }
9298 if(min_step[next.get_mask()][n_x][n_y] > next.step) {
9299 pq.push(next);
9300 }
9301 }
9302 } else {
9303 frame next = f;
9304 next.x = n_x;
9305 next.y = n_y;
9306 next.step++;
9307 if(!min_step.contains(next.get_mask())) {
9308 min_step[next.get_mask()] = vector(m, vector(n, INT_MAX));
9309 }
9310 if(min_step[next.get_mask()][n_x][n_y] > next.step) {
9311 pq.push(next);
9312 }
9313 }
9314 }
9315 }
9316 }
9317 return -1;
9318 }

引用了 leetcode::shortest_path_to_get_all_keys::frame::get_mask(), leetcode::shortest_path_to_get_all_keys::frame::keys, leetcode::shortest_path_to_get_all_keys::START, leetcode::shortest_path_to_get_all_keys::frame::step, leetcode::shortest_path_to_get_all_keys::WALL, leetcode::shortest_path_to_get_all_keys::frame::x , 以及 leetcode::shortest_path_to_get_all_keys::frame::y.

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


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