DFS+剪枝 HDOJ 5323 Solve this interesting problem

题目传送门

 1 /*
 2     题意:告诉一个区间[L,R],问根节点的n是多少
 3     DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - len -1,r];
 4 */
 5 #include <cstdio>
 6 #include <algorithm>
 7 #include <cstring>
 8 #include <cmath>
 9 #include <queue>
10 using namespace std;
11 
12 typedef long long ll;
13 const int INF = 0x3f3f3f3f;
14 const ll INFL = 1e30;
15 ll ans;
16 
17 void DFS(ll l, ll r)  {
18     if (l > r || l < 0) return ;
19     if (l == 0) {
20         ans = min (ans, r); return ;
21     }
22     ll len = r - l + 1;
23     if (l < len)    return ;
24     DFS (l, r + len);
25     if (len != 1)   DFS (l, r + len - 1);
26     DFS (l - len, r);   DFS (l - len - 1, r);
27 }
28 
29 int main(void)  {       //HDOJ 5323 Solve this interesting problem
30     //freopen ("H.in", "r", stdin);
31 
32     ll L, R;
33     while (scanf ("%I64d%I64d", &L, &R) == 2) {
34         if (L == 0)   {
35             if (R >= L) printf ("%I64d
", R);
36             else    puts ("-1");
37             continue;
38         }
39         ans = INFL; DFS (L, R);
40         printf ("%I64d
", (ans == INFL) ? -1 : ans);
41     }
42 
43     return 0;
44 }
编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4685690.html