[UVA1374]Power Calculus【迭代加深】
2018-10-25 17:41
253 查看
Pro
Sol
挺好打的一个搜索题,不过剪枝可能比较难想。
不断地用deep来当做答案,深搜一遍,看能否求出想要的答案,如果可以就不用deep++,否则一直做下去。
Code
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n , deep = -1 , flag , stack[1005] , top; void dfs(int dep , int aim , int use , int cnt) { if(use == aim) { flag = 1; return ; } if(top>dep||top<=0||use<<(dep-cnt)<n) return ; for(int i=1; i<=top; i++) { stack[++top] = use+stack[i]; dfs(dep , aim , use+stack[i] , cnt+1); top--; stack[++top] = use-stack[i]; dfs(dep , aim , use-stack[i] , cnt+1); top--; if(flag) return ; } } void sol(int x) { deep = -1; while(1) { flag = 0; deep++; stack[1] = 1; top = 1; dfs(deep , x , 1 , 0); if(flag) { printf("%d\n",deep); break; } } } int main() { while(1) { scanf("%d",&n); if(!n) break; sol(n); } return 0; }阅读更多
相关文章推荐
- UVA1374(Power Calculus|快速幂计算)=》迭代加深搜
- UVa 1374 - Power Calculus——[迭代加深搜索、快速幂]
- UVA-1374 Power Calculus (迭代加深搜索)
- UVa 1374 - Power Calculus ( IDA* - 迭代加深搜索 )
- Power Calculus UVA - 1374 迭代加深搜索
- UVA - 1374 - Power Calculus(迭代加深)
- UVA UVA - 1374 Power Calculus 快速幂计算(迭代加深搜索)
- UVA 1374 Power Calculus(迭代深搜)
- 1374 - Power Calculus (迭代加深+剪枝)
- UVA 1374 - Power Calculus(迭代深搜)
- uva 1374 - Power Calculus(迭代深搜)
- UVA 1374 Power Calculus(迭代深搜)
- UVA - 1374 Power Calculus 迭代深搜
- poj3134 Power Calculus---迭代加深dfs
- POJ-3134-Power Calculus(迭代加深DFS)
- poj 3134 Power Calculus(迭代加深dfs)
- UVA 1374 迭代加深搜索
- POJ 3134 - Power Calculus 迭代加深搜索(DFSID)
- UVA 1374 - Power Calculus
- Power Calculus UVA - 1374