OpenJ_Bailian - 2787 算24 【思维 + dfs】
2017-06-22 17:44
459 查看
传送门
思路 : 因为要问是否可以算出, 所以需要把所有可能性都列出来, 自然就要想到搜素赛.
AC Code
思路 : 因为要问是否可以算出, 所以需要把所有可能性都列出来, 自然就要想到搜素赛.
AC Code
#include<cstdio> #include<algorithm> #include<cmath> #include<iostream> #include<cstring> #define db double #define CLR(x) memset(x,0,sizeof(x)) using namespace std; const db eps=1e-5; db a[5]; int vis[5]; int dfs(int step) { if(step==1){ if(fabs(a[0]-24)<eps) return 1; else return 0; } for(int i=0;i<step-1;i++){ for(int j=i+1;j<step;j++){ db x=a[i]; db y=a[j]; a[j]=a[step-1]; a[i]=x+y; if(dfs(step-1)) return 1; a[i]=x-y; if(dfs(step-1)) return 1; a[i]=y-x; if(dfs(step-1)) return 1; a[i]=y*x; if(dfs(step-1)) return 1; a[i]=x/y; if(dfs(step-1)) return 1; a[i]=y/x; if(dfs(step-1)) return 1; a[i]=x; a[j]=y; } } return 0; } int main() { while(~scanf("%lf%lf%lf%lf",&a[0],&a[1],&a[2],&a[3])){ CLR(vis); if(a[1]+a[2]+a[3]+a[0]==0){ break; } if(dfs(4)){ printf("YES\n"); } else printf("NO\n"); } }
相关文章推荐
- OpenJ_Bailian 4127 迷宫问题(DFS+BFS)
- OpenJudge 2787 算24(dfs)
- 玲珑杯 Round 24 C 题 【思维好题!!! + bitset标记状态】
- Python错误提示:[Errno 24] Too many open files的分析与解决
- Nginx之——nginx:accept() failed (24: Too many open files)
- Hrbust 2302 Another Tree【思维+Dfs】
- Codeforces 732F Tourist Reform【思维+边双联通+Dfs处理后继问题】好题!
- 多校第三场 1005 HDU 6060 思维贪心+dfs+一维建树
- 【HPU 1195 Mod 】+ 思维 +DFS + 区间剖分
- hdu 2212 dfs 简单的思维问题
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
- OpenJ_Bailian 4118
- 通信基站(dfs回溯,思维)
- DFS+思维-PKU Online Judge-1054:Cube
- Codeforces 813C The Tag Game【思维+Dfs】
- 【BZOJ】1621: [Usaco2008 Open]Roads Around The Farm分岔路口(dfs)
- CodeChef:Chef and Subsequences(思维 & dfs)
- Codeforces Round #225 (Div. 2)(B思维题,E:dfs+树状数组)
- POJ 2488 A Knight's Journey 【dfs + 思维】
- 【bzoj 4581】[Usaco2016 Open]Field Reduction(dfs)