摘要

题目链接 难度等级 完成状态 完成分数 最后编辑时间 失误原因(初次提交分数)
字符变换 ★☆☆☆☆ 答案正确 100 2014/12/23 21:38:37 80(超时)

题意

一串字符,替换成另一串字符,问最小步数(给字典)。

题解

  • 迭代加深搜索(懒得写广搜,这样真的好吗……),没什么特别的
  • 然后写完就发现T了
  • 优化就是改成双向搜索,由于是指数型增长,这样可以提升效率非常多。

代码

1099朴素迭代加深(80分).cpp代码已折叠
展开折叠内容
显示/移除行号
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. string f,t,df[10],dt[10];
  5. int n,depth;
  6. void init();
  7. const bool work (const int&);
  8. const bool dfs(const string & f,const int &depth);
  9. int main()
  10. {
  11. init();
  12. for(int i=1;i<=10;++i)//deeper the depth step by step
  13. {
  14. if(work(i))
  15. {
  16. cout<<i<<endl;
  17. return 0;
  18. }
  19. }
  20. cout<<"NO ANSWER!"<<endl;
  21. return 0;
  22. }
  23. void init()
  24. {
  25. cin>>f>>t;
  26. for(n=1;cin>>df[n]>>dt[n];++n);
  27. --n;
  28. }
  29. const bool work(const int &depth)
  30. {
  31.  ::depth=depth;
  32. return dfs(f,0);
  33. }
  34. const bool dfs(const string & f,const int &depth)
  35. {
  36. if(depth>::depth)
  37. return 0;
  38. if(f==t)
  39. return 1;
  40. for(int i=1;i<=n;++i)
  41. {
  42. int pos=f.find(df[i]);
  43. if(pos==string::npos)
  44. continue;
  45. string nt=f;
  46. nt.replace(pos,df[i].size(),dt[i]);
  47. if(dfs(nt,depth+1))
  48. return 1;
  49. }
  50. return 0;
  51. }
  52.  
1099双向迭代加深.cpp
展开折叠内容
显示/移除行号
  1. #include<iostream>
  2. #include<iterator>
  3. #include<string>
  4. #include<algorithm>
  5. #include<map>
  6. using namespace std;
  7. string f,t,df[10],dt[10];
  8. int n,depth;
  9. void init();
  10. const bool work ();
  11. const bool dfs(const string & f,const int &depth);
  12. const bool rwork();
  13. const bool rdfs(const string & f,const int &depth);
  14. map<string,int> us;
  15. int main()
  16. {
  17. init();
  18. if(work())
  19. {
  20. cout<<us[t];
  21. }
  22. else if(rwork())
  23. {
  24. cout<<depth+5;
  25. }else
  26. cout<<"NO ANSWER!"<<endl;
  27. return 0;
  28. }
  29. void init()
  30. {
  31. cin>>f>>t;
  32. for(n=1;cin>>df[n]>>dt[n];++n);
  33. --n;
  34. }
  35. const bool work()
  36. {
  37.  ::depth=5;
  38. return dfs(f,0);
  39. }
  40. const bool dfs(const string & f,const int &depth)
  41. {
  42. if(depth>::depth)
  43. return 0;
  44. if(us.count(f))
  45. us[f]=min(us[f],depth);
  46. else
  47. us[f]=depth;
  48. if(f==t)
  49. return 1;
  50.  
  51. for(int i=1;i<=n;++i)
  52. {
  53. int pos=f.find(df[i]);
  54. while(pos!=string::npos)
  55. {
  56. string nt=f;
  57. nt.replace(pos,df[i].size(),dt[i]);
  58. if(dfs(nt,depth+1))
  59. return 1;
  60. pos=f.find(df[i],pos+1);
  61. }
  62. }
  63. return 0;
  64. }
  65. const bool rwork()
  66. {
  67. for(depth=1;depth<=5;++depth)
  68. if(rdfs(t,0))
  69. return 1;
  70. return 0;
  71. }
  72. const bool rdfs(const string & f,const int &depth)
  73. {
  74. if(depth>::depth)
  75. return 0;
  76. if(us.count(f))
  77. return 1;
  78. for(int i=1;i<=n;++i)
  79. {
  80. int pos=f.find(dt[i]);
  81. while(pos!=string::npos)
  82. {
  83. string nt=f;
  84. nt.replace(pos,dt[i].size(),df[i]);
  85. if(rdfs(nt,depth+1))
  86. return 1;
  87. pos=f.find(dt[i],pos+1);
  88. }
  89. }
  90. return 0;
  91. }

著作权声明[编辑]

关于[编辑]