poj 2362 Square
2010-08-25 16:42
316 查看
http://acm.pku.edu.cn/JudgeOnline/problem?id=2362
dfs。
dfs。
#include<iostream> #include<algorithm> using namespace std; #define MAX 21 int n; int len; int parts; int max; int sum; int a[MAX]; bool used[MAX]; bool pt(int a,int b) { return a>b; } //cnt: 当前长度 //tot: 找到几段木棒了 //I:为数组下标 bool dfs(int cnt, int tot, int I) { int i; if(tot == parts) return true; for(i=I; i<n; i++) { if(i && !used[i-1] && a[i-1] == a[i]) continue; if(!used[i]) { if(cnt + a[i] < len) { used[i] = true; if(dfs(cnt+a[i], tot, i+1)) return true; used[i] = false; } else if(cnt + a[i] == len) { used[i] = true; if(dfs(0, tot+1, 0)) return true; used[i] = false; } if(cnt == 0) break; } } return false; } int main() { //freopen("in.txt", "r", stdin); int i, t; scanf("%d", &t); while(t--) { scanf("%d", &n); sum = 0; for(i=0; i<n; i++) { scanf("%d", &a[i]); sum += a[i]; used[i] = false; } sort(a, a+n, pt); len = sum / 4; parts = 4; if(sum % 4 == 0 && dfs(0, 1, 0)) printf("yes/n"); else printf("no/n"); } return 0; }
相关文章推荐
- POJ_2362_Square
- POJ 2362 Square
- POJ 2362:Square(DFS)
- 【POJ】2362 Square(DFS)
- poj 2362 Square 搜索
- POJ 2362 square(DFS范例题)
- POJ-2362-Square
- POJ_2362_Square
- poj 2362 Square
- POJ 2362 Square(搜索+剪枝)
- POJ 2362(Square-搜索剪枝1-相对顺序)
- POJ 2362 Square
- POJ 2362 Square
- poj 2362 hdoj 1518 Square(搜索)
- POJ 2362 Square (DFS +剪枝)
- POJ 2362 && HDU 1518 Square(dfs)
- poj 2362 hdoj 1518 Square(搜索)
- poj 2362 Square(dfs, 剪枝)
- poj2362——Square(深搜dfs)
- poj 2362 Square