暴力dfs POJ1426
2013-07-02 21:38
134 查看
/* 题目地址:http://poj.org/problem?id=1426点击打开链接 题目大意:找出仅用0和1 组成的十进制的数构成是出入n的倍数(n<=200) 解题思路:有人直接打表打了200个,因为并不像想的那样数据那么大,有的直接用数学原理。 当然我用了dfs深搜从第一位1开始往下搜,而且是暴力搜索。。。 */ #include<iostream> #include<cstdio> #include<cstring> using namespace std; int p,pos; char res[102]; int dfs(int step,int a) { if(step>100) return 0; //return 0,则回溯,继续。 if(a%p==0&&step>=0&&step<=100) { res[++pos]='\0'; return 1; } else { pos++; res[pos]='0'; if(dfs(step+1,(a*10+0)%p)) return 1; //return 1,则结束。 pos--; pos++; //pos++,--来控制res数组保存的值。 res[pos]='1'; if(dfs(step+1,(a*10+1)%p)) return 1; pos--; return 0; //必须回溯 } } int main() { while(cin>>p&&p) { pos=-1; res[++pos]='1'; dfs(1,1); cout<<res<<endl; } return 0; }
相关文章推荐
- 2014 蓝桥杯 预赛 c/c++ 本科B组 第七题:六角填数【暴力】or【DFS】
- Valid Pattern Lock(dfs + 暴力)
- Street Race_usaco 4.3_spfa+暴力+dfs
- DFS暴力求某集合上的某种的组合情况
- 多校第12场FZU Assign the task(暴力DFS)
- hdu 2616 暴力使用 dfs求最短路径(剪枝有点依稀)
- POJ 1222 EXTENDED LIGHTS OUT【暴力dfs】
- hdu 6165 - 暴力dfs
- hdu 1427 24点暴力dfs
- HDU_6016_(Bestcoder round #92 1002)_(dfs)(暴力)
- HDU 4403 A very hard Aoshu problem(DFS+暴力)
- zoj 1008 暴力枚举求解dfs+优化
- 2017蓝桥杯模拟题 滑动解锁(暴力DFS)
- HDU 4337 King Arthur's Knights(暴力dfs)
- HDU 4272LianLianKan 2012长春网络赛F题(思维) 暴力,dfs可以水过,正解是状态压缩dp
- 2015 CCPC G题 【DFS 暴力】
- UVA 185(暴力DFS)
- hdu 4770 Lights Against Dudely(暴力枚举dfs)
- poj3187Backward Digit Sums(暴力dfs)
- Valid Pattern Lock-ZOJ 暴力DFS搜索