【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons

Posted on 2015-03-02 20:07  LLGemini  阅读(182)  评论(0编辑  收藏  举报

题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m;

随后发篇随笔普及下memset函数的初始化问题。自己也是涨了好多姿势。

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #define INF 0x7fffffff;
 6 using namespace std;
 7 int DP[20100], vis[20100];
 8 int dp(int n, int m)
 9 {
10     if(n <= 0) return INF;
11     if(n >= m) {DP[n] = n-m; return DP[n];}
12     if(!vis[n])
13     {
14         vis[n] = 1;
15         //cout << n << "+++" << endl;
16         DP[n] = min(dp(n-1, m),  dp(n*2, m))+1;
17     }
18     return DP[n];
19 }
20 int main()
21 {
22     int n, m;
23     while(~scanf("%d%d", &n, &m)){
24         memset(DP, 0x3f3f3f3f, sizeof(DP));
25         memset(vis, 0, sizeof(vis));
26         //cout << DP[0] << endl;
27         printf("%d\n", dp(n, m));
28     }
29     return 0;
30 }