LeetCode | 752. Open the Lock BFS技巧题

Youhave 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 wecan turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.

Thelock initially starts at '0000', a string representing the state of the 4 wheels.

Youare given a list of deadends deadends, meaning if the lock displays any of these codes, the wheels of the lockwill stop turning and you will be unable to open it.

Givena target representing the value of the wheels that will unlock thelock, return the minimum total number of turns required to open the lock, or -1if it is impossible.

Example1:

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 displaybecomes the dead end "0102".

Example2:

Input: deadends = ["8888"], target ="0009"

Output: 1

Explanation:

We can turn the last wheel in reverse to move from"0000" -> "0009".

Example3:

Input: deadends =["8887","8889","8878","8898","8788","8988","7888","9888"],target = "8888"

Output: -1

Explanation:

We can't reach the target without getting stuck.

Example4:

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'

这题还是很好理解的,给你一个锁,锁从0000开始,问你能不能得到target的数字,中间会给你一个数组deadends,要求deadends里面的数字组合不能出现,问你可不可以通过一系列的转动得到目标数字,

这题我开始是想用动态规划做的,但是使用动态规划的要处理的数组数据太复杂,不好用数组表示,自底向上没法用数组表示,至顶向下可以考虑,但是这样使用的是递归的方法,会很慢,其实这题使用广度优先遍历就能很好的解决,使用map存储路径,顺便还能用来判断某个节点的距离是否计算了出来,双层循环就能解决8个方向的问题,如何存储四个变量的值呢,使用map<string,int>就能很好的解决,使用set<string>能很好的解决判断值是否在数组里面的功能,以后碰到类似的问题的时候也能使用这个技巧,

以后在做题的时候,当设计到判断最短步骤,最短距离的时候,首先考虑到的不是动态规划,而是广度优先遍历这个技巧!

Set,map,的count功能一样,map[a]=b;如果不存在key为a的值会新建,map.find返回的是map<>:: iterator类型的值,可以直接修改,相当于指针,set不能随机访问,只能insert,map也不能随机访问,但是可以插入,可以使用insert插入

class Solution {

public:

   int openLock(vector<string>& deadends, string target) {

      set<string>theEnd;

      map<string, int> dis;

      queue<string>s;

      stringtmp,t;

      for(string temp : deadends) theEnd.insert(temp);

      dis["0000"]= 0;

      s.push("0000");

 

      while(!s.empty())

      {

            tmp= s.front(); s.pop();

            if(theEnd.count(tmp)) continue;

            intlen = dis[tmp];

            for(int i = 0; i < 4; i++)

                  for(int j = -1; j <= 1; j += 2)

                  {

                       t= tmp;

                       t[i]+= j;

                       if(t[i] > '9') t[i] -= 10;

                       if(t[i] < '0') t[i] += 10;

                      

                      

                       if(!dis.count(t)) dis.insert(pair<string, int>(t, len + 1));

                       else

                       {

                             continue;

                       }

                       if(t == target) return len + 1;

                       s.push(t);

                  }

      }

      return-1;

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值