poj 2362 square-----搜索+剪枝
2016-08-26 18:05
399 查看
题目大意
给出n根木棍,问是否可以首尾相连组成一个正方形。
算法
dfs+剪枝,合理巧妙的剪枝是避免TLE的关键;
1.特判,当木棍总长度不正好是4的倍数时,pass;
2.最长的木棍比边长小时,pass;
3.对木棍长排序,每次都从当前最大到小枚举,在同一条边中,之前放不下的木棍现在也一定放不下;
4.已经确定有解时,跳过剩下的过程;
5.三条边都可以,第四条一定可以。
代码实现
<span style="font-size:18px;">#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
bool f,vis[22];
int n,a[22],sum;
void dfs(int step,int ans,int st) //当前处理第几条边;总长度;当前最长木棍长;
{
if (f) return;
if (step>3)
{
f=1;
return;
}
for (int i=st;i;i--)
{
if(!vis[i]&&ans+a[i]<=sum)
{
ans+=a[i];
vis[i]=true;
if (ans==sum) dfs(step+1,0,n);
else dfs(step,ans,i-1); //更新当前最大木棍长;
if (f) return;
ans-=a[i];
vis[i]=0;
}
}
}
int main()
{
int t;
cin>>t;
while (t)
{
t--;
sum=0;
cin>>n;
memset(a,0,sizeof(a));
for (int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
}
if (sum%4!=0)
{
cout<<"no"<<endl;
continue;
}
sum/=4;
memset(vis,false,sizeof(vis));
sort(a+1,a+n+1);
if (a
>sum)
{
cout<<"no"<<endl;
continue;
}
f=0;
dfs(1,0,n);
if (f) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
</span>
给出n根木棍,问是否可以首尾相连组成一个正方形。
算法
dfs+剪枝,合理巧妙的剪枝是避免TLE的关键;
1.特判,当木棍总长度不正好是4的倍数时,pass;
2.最长的木棍比边长小时,pass;
3.对木棍长排序,每次都从当前最大到小枚举,在同一条边中,之前放不下的木棍现在也一定放不下;
4.已经确定有解时,跳过剩下的过程;
5.三条边都可以,第四条一定可以。
代码实现
<span style="font-size:18px;">#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
bool f,vis[22];
int n,a[22],sum;
void dfs(int step,int ans,int st) //当前处理第几条边;总长度;当前最长木棍长;
{
if (f) return;
if (step>3)
{
f=1;
return;
}
for (int i=st;i;i--)
{
if(!vis[i]&&ans+a[i]<=sum)
{
ans+=a[i];
vis[i]=true;
if (ans==sum) dfs(step+1,0,n);
else dfs(step,ans,i-1); //更新当前最大木棍长;
if (f) return;
ans-=a[i];
vis[i]=0;
}
}
}
int main()
{
int t;
cin>>t;
while (t)
{
t--;
sum=0;
cin>>n;
memset(a,0,sizeof(a));
for (int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
}
if (sum%4!=0)
{
cout<<"no"<<endl;
continue;
}
sum/=4;
memset(vis,false,sizeof(vis));
sort(a+1,a+n+1);
if (a
>sum)
{
cout<<"no"<<endl;
continue;
}
f=0;
dfs(1,0,n);
if (f) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
</span>
相关文章推荐
- POJ 2362(Square-搜索剪枝1-相对顺序)
- POJ 2362 Square (搜索 + 剪枝)
- poj 2362 hdoj 1518 Square(搜索)
- poj 2362 hdoj 1518 Square(搜索)
- poj 2362 Square(dfs, 剪枝)
- poj 2362 Square 搜索
- POJ 2362:Square 觉得这才算深度搜索
- POJ 2362:Square 觉得这才算深度搜索
- POJ 2362 HDOJ 1518 Square DFS剪枝
- POJ 2362 Square (DFS +剪枝)
- poj 2362 Square(简单深搜+剪枝)
- 第八天:poj1010(深度搜索+剪枝,之前完全没有接触过,所以不会,所以多学!!)
- 【DFS迭代加深搜索+剪枝】POJ_1011_Sticks
- poj 2362 Square
- POJ 1011 最小的木棒 (dfs+剪枝|| 搜索好题)
- POJ 1054 The Troublesome Frog 搜索+剪枝
- POJ - 1085 Triangle War 极小极大搜索+alpha-beta剪枝
- poj 2362 Square
- poj 1011/2362 dfs+剪枝(拼木棍)
- poj 1568 (α-β剪枝优化搜索)