• 752. 打开转盘锁


    链接

    752. 打开转盘锁

    题解:

    1. class Solution {
    2. public:
    3. int openLock(vector& deadends, string target) {
    4. std::unordered_set table(deadends.begin(), deadends.end());
    5. if (table.find("0000") != table.end()
    6. || table.find(target) != table.end()) {
    7. return -1;
    8. }
    9. std::queue que;
    10. std::unordered_mapint> distance;
    11. que.push(std::string("0000"));
    12. distance["0000"] = 0;
    13. auto get_next = [](std::string& word)->std::vector {
    14. std::vector result;
    15. for (int i = 0; i < word.size(); ++i) {
    16. char ch = word[i];
    17. if (word[i] == '9') {
    18. word[i] = '0';
    19. } else {
    20. ++word[i];
    21. }
    22. result.push_back(word);
    23. word[i] = ch;
    24. if (word[i] == '0') {
    25. word[i] = '9';
    26. } else {
    27. --word[i];
    28. }
    29. result.push_back(word);
    30. word[i] = ch;
    31. }
    32. return result;
    33. };
    34. while (!que.empty()) {
    35. int size = que.size();
    36. for (int i = 0; i < size; ++i) {
    37. auto f = que.front();
    38. que.pop();
    39. if (f == target) {
    40. return distance[f];
    41. }
    42. for (auto& next : get_next(f)) {
    43. //cout << "next = " << next << endl;
    44. if (table.find(next) != table.end()) {
    45. continue;
    46. }
    47. if (distance.find(next) != distance.end()) {
    48. continue;
    49. }
    50. /*if (next == target) {
    51. return distance[f] + 1;
    52. }*/
    53. distance[next] = distance[f] + 1;
    54. que.push(next);
    55. }
    56. }
    57. }
    58. return -1;
    59. }
    60. };
    1. class Solution {
    2. public:
    3. int openLock(vector& deadends, string target) {
    4. if (target == "0000") {
    5. return 0;
    6. }
    7. unordered_set dead(deadends.begin(), deadends.end());
    8. if (dead.count("0000")) {
    9. return -1;
    10. }
    11. auto num_prev = [](char x) -> char {
    12. return (x == '0' ? '9' : x - 1);
    13. };
    14. auto num_succ = [](char x) -> char {
    15. return (x == '9' ? '0' : x + 1);
    16. };
    17. // 枚举 status 通过一次旋转得到的数字
    18. auto get = [&](string& status) -> vector {
    19. vector ret;
    20. string bak = status;
    21. for (int i = 0; i < 4; ++i) {
    22. char num = status[i];
    23. status[i] = num_prev(num);
    24. ret.push_back(status);
    25. status[i] = num_succ(num);
    26. ret.push_back(status);
    27. status = bak;
    28. }
    29. return ret;
    30. };
    31. queueint>> q;
    32. q.emplace("0000", 0);
    33. unordered_set seen = {"0000"};
    34. int step = 0;
    35. while (!q.empty()) {
    36. auto [status, step] = q.front();
    37. q.pop();
    38. for (auto&& next_status: get(status)) {
    39. if (!seen.count(next_status) && !dead.count(next_status)) {
    40. if (next_status == target) {
    41. //return step + 1;
    42. return step+1;
    43. }
    44. q.emplace(next_status, step + 1);
    45. seen.insert(move(next_status));
    46. }
    47. }
    48. ++step;
    49. }
    50. return -1;
    51. }
    52. };

  • 相关阅读:
    CoCube显示测试笔记
    2022年微信小程序真机调试全流程及10大常见问题处理
    ABB码垛机器人IRB260通讯板维修
    UIAutomator2常用类之UiObject2
    Linux基础知识——(2)vim编辑器
    磷酸酶、转录因子、KRAS ——“不可成药”靶点?
    编译KArchive在windows10下
    逼的测试/开发程序员是不是都这样?心无旁骛......
    引用 Python 中 import 模块
    卧槽,2行代码,让接口性能提升10倍
  • 原文地址:https://blog.csdn.net/INGNIGHT/article/details/132912736