力扣 752. 打开转盘锁 bfs\启发式搜索(A*)

32 篇文章 0 订阅
5 篇文章 0 订阅

https://leetcode-cn.com/problems/open-the-lock/添加链接描述
在这里插入图片描述
思路:显然题目是另外一种类型的最短路问题,最优解可用 b f s bfs bfs解决。

class Solution {
public:
    int openLock(vector<string>& deadends, string target) {
        unordered_map<string,bool> vis;
        for(const string& s:deadends)
            vis[s]=1;
        int ans=-1;
        using pr=pair<string,int>;
        queue<pr> q;
        if(vis.find("0000")!=vis.end())
            return -1;
        q.push(pr("0000",0));
        vis["0000"]=1;
        while(!q.empty())
        {
            pr f=q.front();
            q.pop();
            if(f.first==target)
            {
                ans=f.second;
                break;
            }
            for(int i=0;i<4;i++)
            {
                for(int j=-1;j<=1;j+=2)
                {
                    string tmp=f.first;
                    tmp[i]+=j;
                    if(tmp[i]>'9')
                        tmp[i]='0';
                    else if(tmp[i]<'0')
                        tmp[i]='9';
                    if(vis.find(tmp)==vis.end())
                    {
                        q.emplace(tmp,f.second+1);
                        vis[tmp]=1;
                    }
                }
            }
        }
        return ans;
    }
};

思路二:通过A*求解。关键是如何计算预估函数,我们可以逐一统计从当前字符串到目标字符串的每一位的最小步数,然后把它们加起来即可。

class node{
public:
    int total,cur,toend;
    string s;

    node(int c,string s,const string& target):cur(c),s(move(s))
    {
        toend=GetEstimate(target);
        total=cur+toend;
    }

    bool operator <(const node &n)const
    {
        return total>n.total;
    }

    int GetEstimate(const string &target)
    {
        int ans=0;
        for(int i=0;i<4;i++)
        {
            int tmp=abs(s[i]-target[i]);
            ans+=min(tmp,10-tmp);
        }
        return ans;
    }
};

class Solution {
public:
    int openLock(vector<string>& deadends, string target) {
        if(target=="0000")
            return 0;
        unordered_map<string,bool> vis;
        for(const string& s:deadends)
            vis[s]=1;
        int ans=-1;
        priority_queue<node> q;
        if(vis.find("0000")!=vis.end())
            return -1;
        q.emplace(0,"0000",target);
        vis["0000"]=1;
        while(!q.empty())
        {
            node f=q.top();
            q.pop();
            for(int i=0;i<4;i++)
            {
                for(int j=-1;j<=1;j+=2)
                {
                    string tmp=f.s;
                    tmp[i]+=j;
                    if(tmp[i]>'9')
                        tmp[i]='0';
                    else if(tmp[i]<'0')
                        tmp[i]='9';
                    if(tmp==target)
                        return f.cur+1;
                    if(vis.find(tmp)==vis.end())
                    {
                        vis[tmp]=1;
                        q.emplace(f.cur+1,move(tmp),target);
                    }
                }
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值