UVA - 10344 - 23 out of 5 (暴力)
2015-08-03 10:57
405 查看
23 out of 5
题目传送:23 out of 5解法:直接暴力,记得每次dfs调用都要回溯
AC代码:
[code]#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define INF 0x7fffffff using namespace std; int a[5]; int vis[5]; bool dfs(int n, int ans) { if(n >= 5) { if(ans == 23) return true; return false; } for(int i = 0; i < 5; i ++) { if(!vis[i]) { ans += a[i]; vis[i] = 1; if(dfs(n + 1, ans)) return true; vis[i] = 0; ans -= a[i]; ans -= a[i]; vis[i] = 1; if(dfs(n + 1, ans)) return true; vis[i] = 0; ans += a[i]; ans *= a[i]; vis[i] = 1; if(dfs(n + 1, ans)) return true; vis[i] = 0; ans /= a[i]; } } return false; } int main() { while(scanf("%d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4]) != EOF) { if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0) break; memset(vis, 0, sizeof(vis)); int flag = 0; for(int i = 0; i < 5; i ++) { vis[i] = 1; if(dfs(1, a[i])) { flag = 1; break; } vis[i] = 0; } if(flag == 1) { printf("Possible\n"); } else printf("Impossible\n"); } return 0; }
相关文章推荐
- android XMPP即时通讯客户端
- POJ 1273 --Drainage Ditches【最大流模板 && dinic】
- hihocoder 第五十二周 高斯消元·二【高斯消元解异或方程 难点【模板】】
- 南邮 OJ 1032 三角形数
- shell判断文件
- 20150802厦门大学华为校园提前批招聘机试体验题一:记票统计
- QT快捷键
- 支付宝接口应用及错误整理
- 时间帮助类
- HBase - Java 客户端 | 那伊抹微笑
- 1037:Keep on Truckin'
- githubslidingmenu
- django rest framework quickstart
- 从优化到再优化,最长公共子串
- 从优化到再优化,最长公共子串
- 从优化到再优化,最长公共子串
- Android解决ScrollView嵌套ListView滑动冲突问题
- ubuntu14.04 安装openCV2.4.9
- OllyDbg命令行命令+汇编指令大全
- C语言变长参数的认识以及宏实现