day3.字串变换


双向广搜:双向奔赴才有意义


总结:双向广搜一般用于最小步数模型,当单向bfs状态数太大时,很容易mle和tle,双向广搜是从起点和终点同时搜索,大大减少状态数。例如本题如果单向BFS,\(120^{10}\)是接受不了的,而双向广搜只需要\(2 * 120 ^{5}\),效率大大提高。条件:两个队列,当一个队列为空时,说明在规定步数内已经找不到了。扩展顺序:优先扩展状态数少的一端,而且每次扩展一层。具体解释



字串变换

extend行云流水

#include<bits/stdc++.h>
using namespace std;

const int N = 6;

int n;
string A,B;
string a[N],b[N];


int extend(queue<string> & q,unordered_map<string,int> &da,unordered_map<string,int> &db,string a[N],string b[N])
{
    int d = da[q.front()];
    while(q.size() && da[q.front()] == d)
    {
        auto t = q.front();
        q.pop();
        
        for(int i = 0;i < n;i++)
            for(int j = 0;j < t.size();j++)
                if(t.substr(j,a[i].size()) == a[i])
                {
                    string r = t.substr(0,j) + b[i] + t.substr(j + a[i].size());
                    if(db.count(r)) return da[t] + 1 + db[r];
                    if(da.count(r)) continue;
                    da[r] = da[t] + 1;
                    q.push(r);
                }
    }
    return 11;
}
int bfs()
{
    if(A == B) return 0;
    queue<string> qa,qb;
    unordered_map<string,int> da,db;
    
    qa.push(A),qb.push(B);
    da[A] = db[B] = 0;
    
    int step = 0;
    while(qa.size() && qb.size())
    {
        int t;
        if(qa.size() < qb.size()) t = extend(qa,da,db,a,b);
        else t = extend(qb,db,da,b,a);
        
        if(t <= 10) return t;
        if(++ step == 10) return -1;
    }
    
    return -1;
}
int main()
{
    cin>>A>>B;
    while(cin>>a[n]>>b[n]) n++;
    int t = bfs();
    
    if(t == -1) puts("NO ANSWER!");
    else cout<<t<<endl;
    
    return 0;
}
posted @ 2022-05-13 15:58  Weitongjie  阅读(33)  评论(0)    收藏  举报