鱼C论坛

 找回密码
 立即注册
查看: 262|回复: 1

dfs问题

[复制链接]
发表于 2023-12-3 21:20:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e9+10;
  4. typedef long long ll;
  5. #define INF 0x3f3f3f3f
  6. #define x first
  7. #define y second
  8. // typedef pair<int,int>p;
  9. int dx[]={-1,0,1,0},dy[]={0,-1,0,1}; //上左下右
  10. // 使用dfs减枝
  11. string a,b;
  12. vector<pair<int,string>>A,B;
  13. unordered_map<string ,bool >st;   //表示字符串的状态
  14. void dfs(string a,vector<pair<int,string>>&v,int l) //表示从0开始
  15. {
  16.     st[a]=true;
  17.     v.push_back({l,a});
  18.     for(int i=0;i<a.length();i++)
  19.     {
  20.         string t=a.substr(0,i)+a.substr(i+1);
  21.         if(st[t]||t=="")continue;
  22.         dfs(t,v,l+1);

  23.     }
  24.    
  25. }
  26. bool check(string a,string b)
  27. {
  28.     int a1=stoi(a);
  29.     int b1=stoi(b);
  30.     if(a1%b1==0||b1%a1==0)return true;
  31.     return false;
  32. }
  33. void solve()
  34. {
  35.     cin>>a>>b;
  36.     dfs(a,A,0);
  37.     st.clear();
  38.     dfs(b,B,0);
  39.     int ans=INF;
  40.     for(auto a1:A)
  41.     {
  42.         for(auto b1:B)
  43.         {
  44.             if(check(a1.second,b1.second))
  45.             {
  46.                 ans=min(ans,a1.first+b1.first);
  47.             }
  48.         }
  49.     }
  50.     cout<<(ans==INF?-1:ans);
  51.    
  52.    
  53.    
  54.    
  55. }
  56. int main()
  57. {
  58.     int t;
  59.     t=1;
  60.     while(t--)
  61.     {
  62.         solve();
  63.     }
  64. }
  65. //可以用二进制去做,但是不会,到时候再去补
复制代码

dfs函数是删除字符串的操作,不太懂,还有为什么要l+1;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-3 21:21:09 | 显示全部楼层
在dfs问题中,使用DFS(深度优先搜索)可以遍历所有可能的解空间。但是在实际应用中,为了提高效率,我们需要进行剪枝操作。

下面是一个使用DFS减枝的例子,其中通过交换字符串中的两个字符,找出最小操作次数使得两个字符串相等。
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. using namespace std;
  5. unordered_map<string, bool> st; //表示字符串的状态
  6. int ans = INT_MAX;
  7. void dfs(string a, vector<pair<int, string>>& v, int l) //表示从0开始
  8. {
  9.     if (st[a]) return;
  10.     st[a] = true;
  11.     v.push_back({l, a});
  12.     for (int i = 0; i < a.size(); i++)
  13.     {
  14.         for (int j = i + 1; j < a.size(); j++)
  15.         {
  16.             swap(a[i], a[j]);
  17.             dfs(a, v, l + 1);
  18.             swap(a[i], a[j]); //恢复现场
  19.         }
  20.     }
  21.     v.pop_back();
  22.     st[a] = false;
  23. }
  24. bool check(string a, string b)
  25. {
  26.     return a == b;
  27. }
  28. int main()
  29. {
  30.     string a, b;
  31.     cin >> a >> b;
  32.     vector<pair<int, string>> A, B;
  33.     dfs(a, A, 0);
  34.     st.clear();
  35.     dfs(b, B, 0);
  36.     for (auto a1 : A)
  37.     {
  38.         for (auto b1 : B)
  39.         {
  40.             if (check(a1.second, b1.second))
  41.             {
  42.                 ans = min(ans, a1.first + b1.first);
  43.             }
  44.         }
  45.     }
  46.     if (ans == INT_MAX) cout << -1 << endl;
  47.     else cout << ans << endl;
  48.     return 0;
  49. }
复制代码

在这个例子中,我们通过DFS找出了所有的可能操作,并将每一次操作的次数和字符串保存在一个vector中。然后遍历两个vector,判断当前的字符串是否相等,如果相等则更新最小操作次数。

注意:使用DFS可能会遇到超时的问题,因为DFS会穷尽所有可能的解空间。在实际应用中,可以基于DFS的思路进行剪枝或者使用其他算法如BFS、动态规划等。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 22:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表