ZOJ - 1204 Additive equations(搜索)
2009-05-29 18:15
253 查看
Additive equations
Time Limit: 10 Seconds Memory Limit: 32768 KB
We all understand that an integer set is a collection of distinct integers. Now the question is: given an integer set, can you find all its addtive equations? To explain what an additive equation is, let's look at the following examples:
1+2=3 is an additive equation of the set {1,2,3}, since all the numbers that are summed up in the left-hand-side of the equation, namely 1 and 2, belong to the same set as their sum 3 does. We consider 1+2=3 and 2+1=3 the same equation, and will always output the numbers on the left-hand-side of the equation in ascending order. Therefore in this example, it is claimed that the set {1,2,3} has an unique additive equation 1+2=3.
It is not guaranteed that any integer set has its only additive equation. For example, the set {1,2,5} has no addtive equation and the set {1,2,3,5,6} has more than one additive equations such as 1+2=3, 1+2+3=6, etc. When the number of integers in a set gets large, it will eventually become impossible to find all the additive equations from the top of our minds -- unless you are John von Neumann maybe. So we need you to program the computer to solve this problem.
Input
The input data consists of several test cases.
The first line of the input will contain an integer N, which is the number of test cases.
Each test case will first contain an integer M (1<=M<=30), which is the number of integers in the set, and then is followed by M distinct positive integers in the same line.
Output
For each test case, you are supposed to output all the additive equations of the set. These equations will be sorted according to their lengths first( i.e, the number of integer being summed), and then the equations with the same length will be sorted according to the numbers from left to right, just like the sample output shows. When there is no such equation, simply output "Can't find any equations." in a line. Print a blank line after each test case.
Sample Input
Output for the Sample Input
继续练搜索,不过此题我没有想到一个更好的搜索方法,让搜索完毕后的答案顺序满足题目要求的。。。于是我还是在搜索完毕后再进行一次排序,用了vector<vector<int> >,注意后两个>>之间要有个空格或者/**/, 280ms, 960kb通过
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int set[31];
int curr;
int x[31];
bool flag;
vector<vector<int> > result;
void dfs(int i,int len,int n)
{
if(i==len)
{
if(curr==n)
{
flag=true;
vector<int> v;
int s=0;
for(int k=0;k<len;++k)
if(x[k])
{
v.push_back(x[k]);
s+=x[k];
}
v.push_back(s);
result.push_back(v);
}
}
else
{
if(curr+set[i]<=n)
{
x[i]=set[i];
curr+=set[i];
dfs(i+1,len,n);
x[i]=0;
curr-=set[i];
}
dfs(i+1,len,n);
}
}
bool com(const vector<int> &a,const vector<int> &b)
{
if(a.size()>b.size())return false;
else if(a.size()<b.size())return true;
for(int i=0;i<a.size()-1;++i)
if(a[i]>b[i])return false;
else if(a[i]<b[i])return true;
}
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
curr=0;flag=false;
for(int i=0;i<n;++i)
cin>>set[i];
for(int i=1;i<n;++i)
for(int j=n-1;j>=i;j--)
if(set[j]<set[j-1])
{
int tem=set[j-1];
set[j-1]=set[j];
set[j]=tem;
}
for(int i=2;i<n;++i)
dfs(0,i,set[i]);
if(flag)
{
sort(result.begin(), result.end(), com);
for(int i=0;i<result.size();++i)
{
int j;
for(j=0;j<result[i].size()-1;++j)
{
if(j==0)cout<<result[i][j];
else cout<<"+"<<result[i][j];
}
cout<<"="<<result[i][j]<<endl;
}
result.clear();
}
else cout<<"Can't find any equations."<<endl;
cout<<endl;
}
return 0;
}
Time Limit: 10 Seconds Memory Limit: 32768 KB
We all understand that an integer set is a collection of distinct integers. Now the question is: given an integer set, can you find all its addtive equations? To explain what an additive equation is, let's look at the following examples:
1+2=3 is an additive equation of the set {1,2,3}, since all the numbers that are summed up in the left-hand-side of the equation, namely 1 and 2, belong to the same set as their sum 3 does. We consider 1+2=3 and 2+1=3 the same equation, and will always output the numbers on the left-hand-side of the equation in ascending order. Therefore in this example, it is claimed that the set {1,2,3} has an unique additive equation 1+2=3.
It is not guaranteed that any integer set has its only additive equation. For example, the set {1,2,5} has no addtive equation and the set {1,2,3,5,6} has more than one additive equations such as 1+2=3, 1+2+3=6, etc. When the number of integers in a set gets large, it will eventually become impossible to find all the additive equations from the top of our minds -- unless you are John von Neumann maybe. So we need you to program the computer to solve this problem.
Input
The input data consists of several test cases.
The first line of the input will contain an integer N, which is the number of test cases.
Each test case will first contain an integer M (1<=M<=30), which is the number of integers in the set, and then is followed by M distinct positive integers in the same line.
Output
For each test case, you are supposed to output all the additive equations of the set. These equations will be sorted according to their lengths first( i.e, the number of integer being summed), and then the equations with the same length will be sorted according to the numbers from left to right, just like the sample output shows. When there is no such equation, simply output "Can't find any equations." in a line. Print a blank line after each test case.
Sample Input
3 3 1 2 3 3 1 2 5 6 1 2 3 5 4 6
Output for the Sample Input
1+2=3 Can't find any equations. 1+2=3 1+3=4 1+4=5 1+5=6 2+3=5 2+4=6 1+2+3=6
继续练搜索,不过此题我没有想到一个更好的搜索方法,让搜索完毕后的答案顺序满足题目要求的。。。于是我还是在搜索完毕后再进行一次排序,用了vector<vector<int> >,注意后两个>>之间要有个空格或者/**/, 280ms, 960kb通过
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int set[31];
int curr;
int x[31];
bool flag;
vector<vector<int> > result;
void dfs(int i,int len,int n)
{
if(i==len)
{
if(curr==n)
{
flag=true;
vector<int> v;
int s=0;
for(int k=0;k<len;++k)
if(x[k])
{
v.push_back(x[k]);
s+=x[k];
}
v.push_back(s);
result.push_back(v);
}
}
else
{
if(curr+set[i]<=n)
{
x[i]=set[i];
curr+=set[i];
dfs(i+1,len,n);
x[i]=0;
curr-=set[i];
}
dfs(i+1,len,n);
}
}
bool com(const vector<int> &a,const vector<int> &b)
{
if(a.size()>b.size())return false;
else if(a.size()<b.size())return true;
for(int i=0;i<a.size()-1;++i)
if(a[i]>b[i])return false;
else if(a[i]<b[i])return true;
}
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
curr=0;flag=false;
for(int i=0;i<n;++i)
cin>>set[i];
for(int i=1;i<n;++i)
for(int j=n-1;j>=i;j--)
if(set[j]<set[j-1])
{
int tem=set[j-1];
set[j-1]=set[j];
set[j]=tem;
}
for(int i=2;i<n;++i)
dfs(0,i,set[i]);
if(flag)
{
sort(result.begin(), result.end(), com);
for(int i=0;i<result.size();++i)
{
int j;
for(j=0;j<result[i].size()-1;++j)
{
if(j==0)cout<<result[i][j];
else cout<<"+"<<result[i][j];
}
cout<<"="<<result[i][j]<<endl;
}
result.clear();
}
else cout<<"Can't find any equations."<<endl;
cout<<endl;
}
return 0;
}
相关文章推荐
- ZOJ 2849【瞎暴力的搜索】
- ZOJ 1204 Additive equations
- zoj 2412 搜索
- 【搜索】【CQBZOJ 2468】终极大水题(反素数)
- ZOJ 1649 广度优先搜索与ACM的一道典型搜索题(使用GUI程序模拟搜索过程)
- [ZOJ 1003] Crashing Balloon (dfs搜索)
- ZOJ1204——Additive equations(DFS)
- ZOJ 1033 Ambiguous Dates (简单搜索)
- ZOJ 3768 Continuous Login(迭代搜索||夹逼)
- zoj 1204 Additive equations
- (01背包搜索) zoj 3013
- [ZOJ 2715] Insecure in Prague [搜索]
- zoj 1033 与其说是搜索,不如说是枚举
- zoj 1204
- ZOJ 1204
- (转载)菜鸟拼杀ZOJ心得:[ZZ]递归深度优先搜索
- ZOJ 1204 一个集合能组成多少个等式
- ZOJ 1204 Additive equations
- Zoj 3591 Nim 【博弈】【搜索】
- ZOJ 3814 Sawtooth Puzzle 状态压缩搜索