为什么练样例都过不了
查看原帖
为什么练样例都过不了
1048576
New_Void楼主2025/4/15 22:40
#include <bits/stdc++.h>
using namespace std;
struct node{
    string a;
    int step;
};
queue<node> q;
string arr[100][2];
string a,b;
map<string,bool> vis;
int main(){
    cin>>a>>b;
    int idx=1;
    while (cin>>arr[idx][0]>>arr[idx][1]){
        idx++;
    }
    idx--;
    if(idx==0 && a!=b){
		cout<<"NO ANSWER!";
		return 0;
	}
    q.push({a,0});
    vis[a]=true;
    while(!q.empty()){
        node now=q.front();
        string now2=now.a;
        q.pop();
        if (vis[now.a]) continue;
        vis[now.a]=true;
        if (now.step>10){
            continue;
        }
        if (now.a==b){
            cout<<now.step;
            return 0;
        }
        for (int i=1;i<=idx;i++){
            now.a=now2;
            while (1){
                int f=now.a.find(arr[i][0]);
                if (f==-1){
                    break;
                }
                string str=now2;
                str.replace(f,arr[i][0].size(),arr[i][1]);
                q.push({str,now.step+1});
                now.a[f]=' ';
            }
        }
    }
    cout<<"NO ANSWER!";
    return 0;
}

2025/4/15 22:40
加载中...