码迷,mamicode.com
首页 > 其他好文 > 详细

hdoj 1518(DFS)

时间:2018-07-09 16:25:12      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:std   for   cst   clu   continue   sort   end   out   bool   

#include <iostream>

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int n, a[25], visit[25], side, flag = 0;
bool cmp(int a, int b)
{
return a > b;
}
void dfs(int gs, int len, int k)
{
if (gs >= 3)
{
flag = 1;
return;
}
for (int i = k; i < n; i++)
{
if (flag)
return;
if (visit[i])
continue;
if (len + a[i] == side)
{
visit[i] = 1;
dfs(gs + 1, 0, 0);
visit[i] = 0;
if (flag)
return;
}
else if (len + a[i] < side)
{
visit[i] = 1;
dfs(gs, len + a[i], i + 1);
visit[i] = 0;
if (flag)
return;
}
}

}
int main(int argc, char *argv[])
{
int t, sum;
cin >> t;
while (t--)
{
cin >> n; sum = 0;
for (int i = 0; i<n; i++)
{
cin >> a[i];
sum += a[i];
}
side = sum / 4;
sort(a, a + n, cmp);
if (sum % 4 != 0 || a[0]>side)
{
cout << "no" << endl;
continue;
}
memset(visit, 0, sizeof(visit)); flag = 0;
dfs(0, 0, 0);
if (flag)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}

hdoj 1518(DFS)

标签:std   for   cst   clu   continue   sort   end   out   bool   

原文地址:https://www.cnblogs.com/huluxin/p/9284134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!