1566. 4SUM
时间限制:1秒 内存限制:32兆
题目描述
In this task, you are provided with four sets of integers. Your task consists of selecting one integer from each set, such that their sum is 0. You can assume that such selection exists.
输入格式
The first line of input contains four numbers, a, b, c, and d, separated by a space character, indicating the number of elements in each of the four sets. Each of these numbers is a positive integer 1 <= a, b, c, d <= 500. The following a + b + c + d lines contain the elements, each not smaller than −10000 and not larger than 10000. The elements of the first set are listed first, followed by the elements of the second set, etc.
输出格式
The output consists of the four integers, separated by a space character. The numbers must appear in the order in which they are listed in the input.
样例输入
将样例输入复制到剪贴板
3 2 4 2
5
17
-8
-13
19
6
-9
10
0
-14
7
样例输出
17 -13 10 -14
开始用递归做一直超时,然后跑去问同学,结果同学4遍循环都过,真是气死宝宝了。然后我也写了4次循环果然过了!
但是我不服啊,循环都过,dfs不过,怎么可能。后来终于检查出,原来没有给dfs添加成功便签,它即使检查到正确答案了,还是要跑完所有其他的错误答案!!!
首先是四次循环:
#include <iostream>
#include <map>
#include <cstdio>
#include <vector>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <string>
#include <list>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#define N 501
using namespace std;
int main()
{
int num[4];
for(int i = 0; i<4; i++)
scanf("%d",&num[i]);
int num1[N],num2[N],num3[N],num4[N];
for(int i = 0;i<num[0];i++)
scanf("%d",&num1[i]);
for(int i = 0;i<num[1];i++)
scanf("%d",&num2[i]);
for(int i = 0;i<num[2];i++)
scanf("%d",&num3[i]);
for(int i = 0;i<num[3];i++)
scanf("%d",&num4[i]);
for(int i=0;i<num[0];i++)
{
for(int j=0;j<num[1];j++)
{
for(int k=0;k<num[2];k++)
{
for(int l=0;l<num[3];l++)
{
if(num1[i]+num2[j]+num3[k]+num4[l] == 0)
{
printf("%d %d %d %d\n",num1[i],num2[j],num3[k],num4[l]);
return 0;
}
}
}
}
}
return 0;
}
这里是dfs:
// Problem#: 1566
// Submission#: 4726091
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <map>
#include <vector>
#include <cmath>
#include <string>
#include <set>
#include <sstream>
#include <cstring>
using namespace std;
int a,b,c,d;
int ans[5],num1[501],num2[501],num3[501],num4[501];
bool success = false;
void dfs(int step)
{
if(step == 4)
{
if(ans[1] + ans[2] + ans[3] + ans[4] == 0)
{
cout << ans[1] << " " << ans[2] << " " << ans[3] << " " << ans[4] << endl;
success = true;
}
return;
}
if(success) return;
switch(step)
{
case 0:
for(int i = 0; i<a; i++)
{
ans[1] = num1[i];
dfs(step+1);
ans[1] = -1;
}
break;
case 1:
for(int i = 0; i<a; i++)
{
ans[2] = num2[i];
dfs(step+1);
ans[2] = -1;
}
break;
case 2:
for(int i = 0; i<a; i++)
{
ans[3] = num3[i];
dfs(step+1);
ans[3] = -1;
}
break;
case 3:
for(int i = 0; i<a; i++)
{
ans[4] = num4[i];
dfs(step+1);
ans[4] = -1;
}
break;
}
}
int main()
{
scanf("%d%d%d%d",&a,&b,&c,&d);
for(int i = 0; i<a; i++)
scanf("%d",&num1[i]);
for(int i = 0; i<b; i++)
scanf("%d",&num2[i]);
for(int i = 0; i<c; i++)
scanf("%d",&num3[i]);
for(int i = 0; i<d; i++)
scanf("%d",&num4[i]);
dfs(0);
return 0;
}
心累,但是每次都能发现一点错误也是一种进步嘛,加油加油!