迭代加深搜索

 
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 inline void write(int x)  
 8 {  
 9     if(x<0) putchar('-'),x=-x;  
10     if(x>9) write(x/10);  
11     putchar(x%10+'0');  
12 }
13 
14 inline int read()
15 {
16     int x=0,n=1;
17     char ch=getchar();
18     while(!isdigit(ch))
19     {
20         if(ch=='-')n=-1;
21         ch=getchar();
22     }
23     while(isdigit(ch))
24     {
25         x=(x<<1)+(x<<3)+ch-'0';
26         ch=getchar();
27     }
28     return x*n;
29 }
30 struct team
31 {
32     int n;
33     int now;
34     string ans;
35 }o[609443];//随便开了
36 int n,i,f,tend,end;
37 int main()
38 {
39 //freopen("123.in","r",stdin);
40 //freopen("123.out","w",stdout);
41     m=read();n=read();
42     o[0].n=m;
43     for(i=1;i<=25;i++)
44     {
45         for(tend=end;f<=tend;f++)
46         {
47             if(o[f].n*3==n)
48             {
49                 cout<<o[f].now+1<<endl<<o[f].ans+'H';
50             }
51             if(o[f].n/2==n)
52             {
53                 cout<<o[f].now+1<<endl<<o[f].ans+'O';
54                 return 0;
55             }
56             end++;
57             o[end].n=o[f].n*3;
58             o[end].now=o[f].now+1;
59             o[end].ans=o[f].ans+'H';
60             end++;
61             o[end].n=o[f].n/2;
62             o[end].now=o[f].now+1;
63             o[end].ans=o[f].ans+'O';
64         }
65     }
66 }
 
  
 
 

非常开心的开始了集训,计划如下

看着就头大,而且有十天都是一中的学长讲的,感觉要回到寒假里的状态了。

那么来总结一下刚学到的迭代加深搜索。

它实质上仍是dfs。在dfs搜到底的代价很大和bfs存不完所有状态时可以使用这种办法。

比如这题:

利用迭代加深可以这样

猜你喜欢

转载自www.cnblogs.com/qywyt/p/9263290.html