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. 解决 ajax 跨域

    用两个服务器处理一个项目的代码,其中一台服务器只处理接口请求. 本来PHP可以使用CURL来处理,但是领导不允许使用PHP来处理数据.会影响网站的功能.如果接口端出现问题会导致整个网站或其页面的崩溃, ...

  2. 关于css属性calc对于ie的态度

    做的一个项目,布局的时候用到了max-height:calc(100% - 15px);在谷歌.火狐浏览器,进行下拉的时候,它的父元素会出现垂直滚动条,但是在IE就不可以. 然后在网上找了找,说在它的 ...

  3. php函数strtotime结合date时间修饰语的使用

    下面简单介绍在项目开发中date时间函数和strtotime所遇到的问题,以及解决办法. 原文地址:小时刻个人技术博客 > http://small.aiweimeng.top/index.ph ...

  4. 帆软SQL报异常:多表连接的时候出现错误:未明确定义列

    我刚开始的代码: select dm_veh_jdcgz_mx.DAY_ID ,--日期 dm_veh_jdcgz_mx.GLBM ,--管理部门ID dm_veh_jdcgz_mx.SFZMHM , ...

  5. MFC非模态添加进程控件方法一(线程方法)

    由于非模态对话框的自己没有消息循环,创建后无法进行消息处理.需要和父窗口共用消息循环.如果单独在子窗口进行控件由于自己没有单独的消息循环,更新是无法进行的. 如果在父窗口更新控件会造成程序假死.如以下 ...

  6. 《AngularJS学习整理》系列分享专栏

    <AngularJS学习整理>系列分享专栏   <AngularJS学习整理>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/re ...

  7. C# 使用cmd

    public static string cmd(String command) //向cmd()传入命令行,传入"exit"则退出cmd.exe. { Process p = n ...

  8. vim 智能提示插件 javacomplete安装

    *** 从https://github.com/vim-scripts/javacomplete网站中下载javacomplete *** 新建javacomplete-master文件夹,将java ...

  9. python的第一个程序“Hello,World”,传闻要想学好新语言....

    传闻要想学好新语言,第一个程序必须是“Hello,World”...O(∩_∩)O哈哈~ 下面附上代码: # -*- coding:utf-8 -*- print("Hello,World& ...

  10. 推荐 的FPGA设计经验(1)组合逻辑优化

    主要内容摘自Quartus prime Recommended Design Practices For optimal performance, reliability, and faster ti ...