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:

  1. The length of deadends will be in the range [1, 500].
  2. target will not be in the list deadends.
  3. Every string in deadends and the string target will 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]的更多相关文章

  1. [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 ...

  2. LeetCode 752. Open the Lock

    原题链接在这里:https://leetcode.com/problems/open-the-lock/ 题目: You have a lock in front of you with 4 circ ...

  3. [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', ...

  4. [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', ' ...

  5. Java实现 LeetCode 753 破解保险箱(递归)

    753. 破解保险箱 有一个需要密码才能打开的保险箱.密码是 n 位数, 密码的每一位是 k 位序列 0, 1, -, k-1 中的一个 . 你可以随意输入密码,保险箱会自动记住最后 n 位输入,如果 ...

  6. Leetcode: Find Permutation(Unsolve lock problem)

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  7. [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', ...

  8. Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)

    Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  9. 【LeetCode】752. Open the Lock 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  10. LeetCode 752:打开转盘锁 Open the Lock

    题目: 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋转:例如把 ' ...

随机推荐

  1. Swift_ScrollView _ API详解

    Swift_ScrollView _ API详解 GitHub class ViewController: UIViewController,UIScrollViewDelegate { var sc ...

  2. 【centOS7.3 彻底卸载MySQL】

    废话不多说,直接正面刚. 1.删除MySQL yum remove mysql mysql-server mysql-libs mysql-server; 执行后继续查找相关文件 find / -na ...

  3. 洛谷P3812 【模板】线性基

    题目背景 这是一道模板题. 题目描述 给定n个整数(数字可能重复),求在这些数中选取任意个,使得他们的异或和最大. 输入输出格式 输入格式: 第一行一个数n,表示元素个数 接下来一行n个数 输出格式: ...

  4. 关于Hibernate的部分知识总结

    [部分内容参考地址:https://www.cnblogs.com/woniu2219/p/7111857.html] Hibernate Hibernate是一个开放源代码的对象关系映射框架,它对J ...

  5. 关于js代码位置的第一次总结

    最近在学习dom树节点操作时,发现查找结点总是返回null,原因在于将js代码放在了head里,因为页面是从上往下逐行加载,在还未加载相关节点时当然查找不到,返回值为null. 而对于另一句老生常谈的 ...

  6. 导入jar包和创建jar文件

    具体步骤   导入jar包 1.在第一个工程中编写工具类并运行生成.class文件 2.在myeclipse工具栏找到open in 文件夹图标找到.class文件所在的包,将其全部复制到某个盘符下( ...

  7. echarts 多图任意布局案例

    随着文档的更新,布局越来越个性化,完全可以替代整个元素界面,随意布局展示数据,下面发布一个小的文件 源码文件:https://files.cnblogs.com/files/mobeisanghai/ ...

  8. Thinkphp5 使用composer中seeder播种机

    前因: 前几天,客户要求做一个会员问答的系统,我就按流程做了,到了需要调用数据库数据时,觉得一个个添加又有点笨~ 解决过程: 后来查了查手册,看看国外blog案例,我搞出来了个不错的方法~~~ 我的使 ...

  9. eclipse创建maven项目及Javaweb项目

    1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显示创建maven项目的窗口 3.在搜索框中搜索“web”,选择,n ...

  10. thinkphp3.2.3 HTML 页面跳转

    1.   http://域名/index.php(入口文件)/模块/控制器/方法 2.{:U('控制器/方法')}