Power Calculus UVA - 1374 迭代加深搜索
2018-10-15 00:18
513 查看
迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大
题目:题目链接
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <cstring> #include <vector> #include <string> #include <queue> #include <map> #include <set> #define FRER() freopen("in.txt", "r", stdin); #define INF 0x3f3f3f3f using namespace std; const int maxn = 10000 + 5; int num[maxn], vis[maxn], temp[50], maxd, n; bool dfs(int, int); int main() { //FRER() ios::sync_with_stdio(0); cin.tie(0); temp[0] = 1; for(int i = 1; i < 32; ++ i) temp[i] = temp[i - 1] << 1; while(cin >> n && n) { memset(vis, 0, sizeof(vis)); for(maxd = 0; ; ++maxd) if(dfs(0, 1)) break; cout << maxd << endl; } return 0; } bool dfs(int cur, int s) { if(cur == maxd && s == n) return true; if(cur > maxd) return false; if(s * temp[maxd - cur] < n) return false; num[cur] = s; vis[s] = 1; for(int i = 0; i <= cur; ++i) { int u = num[i] + s; if(!vis[u]) { vis[u] = 1; if(dfs(cur + 1, u)) return true; vis[u] = 0; } u = abs(num[i] - s); if(!vis[u]) { vis[u] = 1; if(dfs(cur + 1, u)) return true; vis[u] = 0; } } vis[s] = 0; return false; }
相关文章推荐
- UVA UVA - 1374 Power Calculus 快速幂计算(迭代加深搜索)
- UVa 1374 - Power Calculus ( IDA* - 迭代加深搜索 )
- UVa 1374 - Power Calculus——[迭代加深搜索、快速幂]
- UVA-1374 Power Calculus (迭代加深搜索)
- [UVA1374]Power Calculus【迭代加深】
- UVA1374(Power Calculus|快速幂计算)=》迭代加深搜
- UVA - 1374 - Power Calculus(迭代加深)
- UVA 1374 迭代加深搜索
- LA 3621 / POJ 3134 Power Calculus (迭代加深搜索)
- UVA - 1374 Power Calculus 迭代深搜
- POJ 3134 Power Calculus 迭代加深搜索
- UVA 1374 ——Power Calculus(IDA*搜索)
- UVA 1374 - Power Calculus(迭代深搜)
- POJ 3134 - Power Calculus 迭代加深搜索(DFSID)
- UVA 1374 Power Calculus(迭代深搜)
- uva 1374 - Power Calculus(迭代深搜)
- 1374 - Power Calculus (迭代加深+剪枝)
- UVA 1374 Power Calculus(迭代深搜)
- UVA 11212 Editing a Book [迭代加深搜索IDA*]
- POJ-3134-Power Calculus(迭代加深DFS)