当前位置:网站首页>796 · 开锁
796 · 开锁
2022-07-03 08:17:00 【yinhua405】
你面前有一个有四个圆形轮子的锁。每个轮子有10个槽:‘0’
,‘1’
,‘2’
,‘3’
,‘4’
,‘5’
,‘6’
,‘7’
,‘8’
,‘9’
。轮子可以自由旋转并环绕:例如,我们可以把‘9’
变成‘0’
,或者‘0’
变成‘9’
。每个动作包括转动一个轮子一个槽。
锁最初是‘0000’
开始的,这是一个表示四个轮子状态的字符串。
你被给了一个死锁
的列表,意思是如果锁显示了这些代码中的任何一个,锁的轮子将停止转动,你将无法打开它。
给定一个表示将解锁锁的轮子的值的目标
,返回打开锁所需要的最小总次数,如果不可能,则返回-1。
一起战拖呀!
微信加【jiuzhang0607】备注【战拖】即可进入官方刷题群,团战offer!
1.死锁的列表长度将在[1,500]
范围内。
2.目标
不在死锁列表中。
3.每一个字符串在死锁列表
和目标字段
将是一串4位数字有10000种可能性从'0000'到'9999'。
样例
样例 1:
给出死锁列表=[“0201”、“0101”、“0102”、“1212”、“2002”),目标= " 0202 "
返回 6
解释:
一系列有效的动作将是“0000”->“1000”->“1100”->“1200”->“1201”->“1202”->“0202”。
请注意,像“0000”->“0001”->“0002”->“0102”->“0202”的序列将是无效的,
因为锁的轮子在显示器变成了“0102”后卡住了。
样例 2:
给出死锁列表 = ["8888"], 目标 = "0009"
返回 1
解释:
我们可以从“0000”->“0009”转到最后一个转轮。
char preNum(char x)
{
return x == '0' ? '9' : x - 1;
}
char bakNum(char x)
{
return x == '9' ? '0' : x + 1;
}
vector<string> getStr(string &str)
{
vector<string>ret;
for (int i = 0; i < 4; i++)
{
char c = str[i];
str[i] = preNum(c);
ret.push_back(str);
str[i] = bakNum(c);
ret.push_back(str);
str[i] = c;
}
return ret;
}
int openLock(vector<string> &deadends, string &target) {
// Write your code here
if (target == "0000")
{
return -1;
}
set<string> deaSet;
for (int i = 0; i < deadends.size(); i++)
{
if (deadends[i] == "0000")
{
return -1;
}
deaSet.insert(deadends[i]);
}
set<string> queueSet;
set<string> tmpQueSet;
set<string>visitedSet;
queueSet.insert("0000");
visitedSet.insert("0000");
int step = 0;
while (queueSet.size()>0)
{
step++;
for (auto it : queueSet)
{
vector<string> retVec = getStr(it);
for (auto itt : retVec)
{
if (visitedSet.count(itt) > 0 || deaSet.count(itt) >0 )
{
continue;
}
if (itt == target)
{
return step;
}
visitedSet.insert(itt);
tmpQueSet.insert(itt);
}
}
queueSet = tmpQueSet;
tmpQueSet.clear();
}
return -1;
}
边栏推荐
猜你喜欢
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
C language - Introduction - essence Edition - take you into programming (I)
the installer has encountered an unexpected error installing this package
CLion-Toolchains are not configured Configure Disable profile问题解决
Haproxy+kept build 01
ArrayList
Wpf: solve the problem that materialdesign:dialoghost cannot be closed
JS common basic case sorting (continuous update)
Viz artist advanced script video tutorial -- stringmap use and vertex operation
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
随机推荐
Abstract classes and interfaces
tslib库的移植
Basic operation and process control 2
Ue5 opencv plug-in use
swagger文档配置
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
Ilruntime learning - start from scratch
P1596 [USACO10OCT]Lake Counting S
Golang中删除字符串的最后一个字符
P2622 关灯问题II(状态压缩 搜索)
C语言-入门-精华版-带你走进编程(一)
An intern's journey to cnosdb
Encoding and decoding of golang URL
【更新中】微信小程序学习笔记_3
C course design employee information management system
多旅行商问题——公式和求解过程概述
Shader foundation 01
jsutlis
Use filechannel to copy files
详解sizeof、strlen、指针和数组等组合题