记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof

 

题目传送门

 1 /*  2  记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数  3  ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + 1);前者表示蛋没碎,则往高处(y-i)搜索  4  后者表示蛋碎了,往低处(i-1)方向搜索  5  这样写不好,每次memset (dp)就会超时:(  6  详细解释:http://blog.csdn.net/fulongxu/article/details/27110435  7 */  8 #include <cstdio>  9 #include <iostream> 10 #include <algorithm> 11 #include <cstring> 12 #include <cmath> 13 using namespace std; 14 15 const int MAXN = 1e3 + 10; 16 const int INF = 0x3f3f3f3f; 17 int dp[11][MAXN]; 18 19 int DFS(int x, int y) 20 { 21 if (dp[x][y]) return dp[x][y]; 22 if (x == 1) {dp[x][y] = y; return y;} 23 if (y <= 2) {dp[x][y] = y; return y;} 24 25 int ans = INF; 26 for (int i=2; i<y; ++i) 27  { 28 int tmp = max (DFS (x, y-i) + 1, DFS (x-1, i-1) + 1); 29 ans = min (ans, tmp); 30  } 31 32 return dp[x][y] = ans; 33 } 34 35 int main(void) //URAL 1223 Chernobyl’ Eagle on a Roof 36 { 37 //freopen ("Z.in", "r", stdin); 38 39 int n, m; 40 while (scanf ("%d%d", &n, &m) == 2) 41  { 42 if (!n && !m) break; 43 if (n > 10) n = 10; 44 45 printf ("%d\n", DFS (n, m)); 46  } 47 48 return 0; 49 }

 

你可能感兴趣的:(DFS)