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

#include <leetcode.h>

静态 Public 成员函数

static int openLock (vector< string > &deadends, const string &target)
 

详细描述

在文件 leetcode.h2910 行定义.

成员函数说明

◆ openLock()

int leetcode::open_the_lock::Solution::openLock ( vector< string > &  deadends,
const string &  target 
)
static

在文件 leetcode.cpp8059 行定义.

8059 {
8060 unordered_set<string> deads;
8061 unordered_set<string> vis;
8062 for(auto &deadend: deadends) {
8063 deads.insert(deadend);
8064 }
8065 auto get_nexts = [](const string &code) {
8066 vector ans(8, code);
8067 ans[0][0] = (ans[0][0] - '0' + 10 + 1) % 10 + '0';
8068 ans[1][0] = (ans[1][0] - '0' + 10 - 1) % 10 + '0';
8069 ans[2][1] = (ans[2][1] - '0' + 10 + 1) % 10 + '0';
8070 ans[3][1] = (ans[3][1] - '0' + 10 - 1) % 10 + '0';
8071 ans[4][2] = (ans[4][2] - '0' + 10 + 1) % 10 + '0';
8072 ans[5][2] = (ans[5][2] - '0' + 10 - 1) % 10 + '0';
8073 ans[6][3] = (ans[6][3] - '0' + 10 + 1) % 10 + '0';
8074 ans[7][3] = (ans[7][3] - '0' + 10 - 1) % 10 + '0';
8075 return ans;
8076 };
8077 queue<pair<int, string>> q;
8078 q.push({0, "0000"});
8079 while(!q.empty()) {
8080 auto [step, code] = q.front();
8081 if(code == target) {
8082 return step;
8083 }
8084 vis.insert(code);
8085 q.pop();
8086 if(deads.contains(code)) {
8087 continue;
8088 }
8089 auto nexts = get_nexts(code);
8090 for(auto &next: nexts) {
8091 if(!vis.contains(next)) {
8092 vis.insert(next);
8093 if(next == target) {
8094 return step + 1;
8095 }
8096 q.push({step + 1, next});
8097 }
8098 }
8099 }
8100 return -1;
8101 }

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


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