hdu1518 bjfuoj1042 zoj1909 poj2362 经典的搜索加剪枝

bjfuoj的测试数据最水,用很简单的方法一下就过了,又调了好长时间,才过掉其它OJ上的这道题目~

/*
* hdu1518/win.cpp
* Created on: 2011-11-8
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MAXN = 25;
int lens[MAXN];
bool visited[MAXN];
int N, ave, cur_tol_len, totallen;

inline bool cmp(const int &a, const int &b) {
return a > b;
}

bool myfind(int step, int num) {
if (num > 3) {
return true;
}
if (step >= N) {
return false;
}
if (!visited[step] && (lens[step] + cur_tol_len) <= ave * num) {
visited[step] = true;
cur_tol_len += lens[step];
if(cur_tol_len == ave * num) {
if(myfind(0, num + 1)) {
return true;
}
}else if (myfind(step + 1, num)) {
return true;
}
visited[step] = false;
cur_tol_len -= lens[step];
}
return myfind(step + 1, num);
}

bool judge() {
if (N < 4 || totallen % 4 != 0) {
return false;
}
ave = totallen / 4;
for (int i = 0; i < N; i++) {
if (lens[i] > ave) {
return false;
}
}
sort(lens, lens + N, cmp);
memset(visited, false, sizeof(visited));
cur_tol_len = 0;
return myfind(0, 1);
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &N);
totallen = 0;
for (int i = 0; i < N; i++) {
scanf("%d", &lens[i]);
totallen += lens[i];
}
if (judge()) {
puts("yes");
} else {
puts("no");
}
}
return 0;
}



posted @ 2011-11-08 20:25  moonbay  阅读(280)  评论(0编辑  收藏  举报