[leetcode-753-Open the Lock]
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.
The lock initially starts at '0000', a string representing the state of the 4 wheels.
You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.
Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.
Example 1:
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
Output: 6
Explanation:
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
because the wheels of the lock become stuck after the display becomes the dead end "0102".
Example 2:
Input: deadends = ["8888"], target = "0009"
Output: 1
Explanation:
We can turn the last wheel in reverse to move from "0000" -> "0009".
Example 3:
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
Output: -1
Explanation:
We can't reach the target without getting stuck.
Example 4:
Input: deadends = ["0000"], target = "8888"
Output: -1
Note:
- The length of
deadendswill be in the range[1, 500]. targetwill not be in the listdeadends.- Every string in
deadendsand the stringtargetwill be a string of 4 digits from the 10,000 possibilities'0000'to'9999'.
思路:
BFS广度优先搜索。当前状态到下一个状态有4*2 = 8种可能。并随时记录状态是否访问,剪枝优化。
int openLock(vector<string>& deadends, string target)
{
int ret = ;
unordered_set<string> dead(deadends.begin(),deadends.end());
unordered_set<string> visited;
queue<string>que;
if(dead.find("")!=dead.end()) return -;
visited.insert("");
que.push(""); while(!que.empty())
{
int sz = que.size();
for(int i=;i<sz;i++)
{
string temp = que.front();que.pop();
vector<string>next = getNext(temp);
for(auto n : next)
{
if(n == target)return ret+;
if(visited.find(n)!=visited.end()) continue;
if(dead.find(n)==dead.end())
{
que.push(n);
visited.insert(n);
}
}
}
ret++;
}
return -;
} vector<string> getNext(string str)
{
vector<string> next;
for(int i=;i<;i++)
{
string tmp = str;
tmp[i] = (str[i]+-'')%+'';
next.push_back(tmp);
tmp[i] = (str[i]--''+)%+'';
next.push_back(tmp);
}
return next;
}
参考:
https://discuss.leetcode.com/topic/114807/bfs-solution-c
[leetcode-753-Open the Lock]的更多相关文章
- [LeetCode] 753. Cracking the Safe 破解密码
There is a box protected by a password. The password is n digits, where each letter can be one of th ...
- LeetCode 752. Open the Lock
原题链接在这里:https://leetcode.com/problems/open-the-lock/ 题目: You have a lock in front of you with 4 circ ...
- [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] 0752. Open the Lock 打开转盘锁
题目 You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', ' ...
- Java实现 LeetCode 753 破解保险箱(递归)
753. 破解保险箱 有一个需要密码才能打开的保险箱.密码是 n 位数, 密码的每一位是 k 位序列 0, 1, -, k-1 中的一个 . 你可以随意输入密码,保险箱会自动记住最后 n 位输入,如果 ...
- Leetcode: Find Permutation(Unsolve lock problem)
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- [LeetCode] 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之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)
Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- 【LeetCode】752. Open the Lock 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 752:打开转盘锁 Open the Lock
题目: 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋转:例如把 ' ...
随机推荐
- IPC进程间通信---共享内存
共享内存 共享内存:共享内存就是分配一块能被其它进程访问的内存.每个共享内存段在内核中维护着一个内部结构shmid_ds, 该结构定义在头文件linux/shm.h中,其结构如下: struct sh ...
- java获取客户端信息
创建JSP页面 clientinfo <%@page import="java.util.StringTokenizer"%> <%@ page language ...
- linux下的学习之路下的小困难
centos下源码安装python3wget --no-check-certificate https://www.python.org/ftp/python/3.6.2/Python-3.6.2.t ...
- MySQL/MariaDB学习笔记——mysql.user表中存在多个root用户问题理解
mysql.user表中存在多个root用户问题 问题描述:使用 SELECT host,user FROM mysql.user 发现mysql.user表中存在三个root用户,如下 持着对中几个 ...
- JSP/Servlet开发——第十一章 Ajax交互扩展
1. jQuery实现Ajax的方法: ●除了$.ajax()方法以外,jQuery还提供了其他多种更简单的 Ajax 实现方法,如$.get().$.post().$.getJSON().对象.lo ...
- 小心使用replicate_do_db和replicate_ignore_db
内容来源于网络 使用replicate_do_db和replicate_ignore_db时有一个隐患,跨库更新时会出错 如设置 replicate_do_db=testuse mysql;updat ...
- QQ空间|qq人气号怎么赚钱?
回报,付出的终极诉求,咱不论情怀. 在<怎么做一个QQ人气号>中,笔者大致提及,打造人气空间的流程,这里简单剖析下QQ人气空间的盈利模式. 关键词:转让,出售,广告,微商,合作,网红,接推 ...
- 大数据&人工智能&云计算
仅从技术上讲大数据.人工智能都包含工程.算法两方面内容: 一.大数据: 工程: 1)云计算,核心是怎么管理大量的计算机.存储.网络. 2)核心是如何管理数据:代表是分布式存储,HDFS 3)核心是如何 ...
- mfc和qt的区别
注:引用来源 http://wenda.chinabaike.com/b/30934/2013/1208/707410.html QT使用的编译器是MinGW,即Linux下的GCC移植到window ...
- 网络文件系统nfs在ubuntu16.04的安装
1.搜索nfs-sudo apt-cache search nfs- 2.安装sudo apt-get install nfs-kernel-server 3.配置:/etc/exports /hom ...