用的DFS,#5WA,显示找不到
查看原帖
用的DFS,#5WA,显示找不到
605777
Jasonde1024楼主2022/7/11 08:48
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

string a, b;
vector<string> before;
vector<string> after;
bool found = false;
int min_d = 120999;

void dfs(string cur_str, int depth) {
	//if (cur_str[0] != b[0]) return;
	cerr << cur_str << " " << depth << endl;
	if (cur_str == b) {
		if (depth < min_d) min_d = depth;
		found = true;
	}
	if (depth == 10) {
		if (cur_str != b) return;
	}
	for (int i = 0; i < before.size(); ++i) {
		auto f = cur_str.find(before[i]);
		if (f != string::npos) {
			string temp = cur_str;
			temp.replace(f, before[i].size(), after[i]);
			dfs(temp, depth+1);
		}
	}
}

int main() {
	cin >> a >> b;
	string temp_a, temp_b;
	while (cin >> temp_a >> temp_b) {
		before.push_back(temp_a);
		after.push_back(temp_b);
	}
	dfs(a, 0);
	if (!found) {
		cout << "NO ANSWER!" << endl;
		return 0;
	}
	cout << min_d << endl;
}
2022/7/11 08:48
加载中...