leetcode 752. 打开转盘锁
地址 https://leetcode-cn.com/problems/open-the-lock/
你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 。每个拨轮可以自由旋转:例如把 '9' 变为 '0','0' 变为 '9' 。每次旋转都只能旋转一个拨轮的一位数字。
锁的初始数字为 '0000' ,一个代表四个拨轮的数字的字符串。
列表 deadends 包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。
字符串 target 代表可以解锁的数字,你需要给出最小的旋转次数,如果无论如何不能解锁,返回 -1。
示例 : 输入:deadends = ["","","","",""], target = ""
输出:
解释:
可能的移动序列为 "" -> "" -> "" -> "" -> "" -> "" -> ""。
注意 "" -> "" -> "" -> "" -> "" 这样的序列是不能解锁的,
因为当拨动到 "" 时这个锁就会被锁定。
示例 : 输入: deadends = [""], target = ""
输出:
解释:
把最后一位反向旋转一次即可 "" -> ""。
示例 : 输入: deadends = ["","","","","","","",""], target = ""
输出:-
解释:
无法旋转到目标数字且不被锁定。
示例 : 输入: deadends = [""], target = ""
输出:-
提示: 死亡列表 deadends 的长度范围为 [, ]。
目标数字 target 不会在 deadends 之中。
每个 deadends 和 target 中的字符串的数字会在 , 个可能的情况 '' 到 '' 中产生。
题解 BFS 宽度优先搜索 可以更快的找到 最短路径
然后就是BFS的框架 需要注意的小细节有两点
1 使用哈希而不是vector 进行deadends的比对
2 一旦搜索到目标就可以退出
代码
class Solution {
public: queue<string> q;
int visit[];
int g_ret =-;
string g_target; void bfdInner(string& s, int count, unordered_set<string>& deadends)
{
if(deadends.count(s) != ) return; if(s == g_target) {
g_ret = count;
return;
} int num = atoi(s.c_str());
if (visit[num] > count) {
visit[num] = count;
q.push(s);
}
} void bfs(string& s, int count, int idx, unordered_set<string>& deadends)
{
string s1 = s, s2 = s;
if(g_ret != - ) return; if (s[idx] == '') { s1[idx] = ''; s2[idx] = ''; }
else if (s[idx] == '') { s1[idx] = ''; s2[idx] = ''; }
else { s1[idx]++; s2[idx]--; } bfdInner(s1, count, deadends);
bfdInner(s2, count, deadends); } int openLock(vector<string>& deadends, string target) { for (int i = ; i < ; i++) {
visit[i] = ;
}
unordered_set<string> deadlock(deadends.begin(), deadends.end()); g_target = target; string start = "";
if(deadlock.count("") != ) return -; visit[] = ;
q.push(start); //bfs
while (!q.empty()) {
string numStr = q.front();
q.pop(); int count = visit[atoi(numStr.c_str())]; for (int i = ; i < ; i++) {
bfs(numStr, count + , i, deadlock);
} } return g_ret;
} };
leetcode 752. 打开转盘锁的更多相关文章
- Java实现 LeetCode 752 打开转盘锁(暴力)
752. 打开转盘锁 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋 ...
- Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)
Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- LeetCode 752:打开转盘锁 Open the Lock
题目: 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋转:例如把 ' ...
- [LeetCode] 0752. Open the Lock 打开转盘锁
题目 You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', ' ...
- [Swift]LeetCode752. 打开转盘锁 | Open the Lock
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- leetcode752. 打开转盘锁
我们可以将 0000 到 9999 这 10000 状态看成图上的 10000 个节点,两个节点之间存在一条边,当且仅当这两个节点对应的状态只有 1 位不同,且不同的那位相差 1(包括 0 和 9 也 ...
- [LeetCode] 752. Open the Lock 开锁
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- LeetCode 752. Open the Lock
原题链接在这里:https://leetcode.com/problems/open-the-lock/ 题目: You have a lock in front of you with 4 circ ...
- 4、BFS算法套路框架——Go语言版
前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...
随机推荐
- JS---DOM---点击操作---part2---14个案例
案例1:点击按钮禁用文本框 <input type="button" value="禁用文本框" id="btn" /> < ...
- 搭建Nginx四层反向代理
需求背景: 前段时间公司因为业务需求需要部署一个正向代理,我已经分享出来了https://www.cnblogs.com/Dfengshuo/p/11911406.html,现有因架构个更改,需要再加 ...
- 【Cocos谁学谁会】制作会跑动的地板
版权申明: 本文原创首发于以下网站,您可以自由转载,但必须加入完整的版权声明 博客园:https://www.cnblogs.com/MogooStudio/ csdn博客:https://blog. ...
- ORA-14061: 不能更改索引分区列的数据类型或长度
修改分区表主键时报错: 在行: 2 上开始执行命令时出错 -alter table KC23 modify AAZ210 VARCHAR2(50)错误报告 -SQL 错误: ORA-14061: 不能 ...
- PHP计算二维数组指定元素的和
array_sum(array_column($arr, 'num')); //计算二维数组指定元素的和 $arr = [ [ 'id'=>1, 'num'=>3, ], [ 'id'=& ...
- Tomcat乱码或异常
一.控制台乱码 原因:Tomcat与Windows编码不一致导致 解决办法:首先找到conf/logging.properties文件,然后打开后找到“java.util.logging.Consol ...
- 「Shimo使用指南」mac支持pptp协议的小软件
Mac的好多小伙伴在访问网络设备时觉得远程连接不方便,例如ssh,***登陆都不是很方便,后来又安装了open*** forMac.ISSH等客户端,使用后发现不是很稳定,断线后很久都无法连接等缺点, ...
- github二级域名配置
首先打开GitHub并登上你的GitHub账号 新建仓库 点击settings 接下来就是操作git往这个仓库存文件,该域名会访问index.html文件
- canvas应用
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 2019-2020-1 20199305《Linux内核原理与分析》第八周作业
可执行程序的工作原理 (一)ELF目标文件 (1)什么是ELF? 这里先提一个常见的名词"目标文件",是指编译器生成的文件.ELF(Executable and Linkable ...