-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint all subsets using recursion
63 lines (45 loc) · 1.24 KB
/
print all subsets using recursion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define endl "\n"
#define ll long long
#define int long long
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cin.exceptions(cin.failbit);cout.tie(NULL);
using namespace std;
void findsubset(int reqdx,int reqdy,int k,int sumx,int sumy,vector<pair<int,int> > v,vector<pair<int,int> > temp,int i)
{
if(i==k)
return ;
if(reqdx==sumx && reqdy==sumy)
{
for(int i=0;i<temp.size();i++)
{
cout << temp[i].first << " " << temp[i].second << endl;
}
return ;
}
findsubset(reqdx,reqdy,k,sumx,sumy,v,temp,i+1);
temp.push_back({v[i].first,v[i].second});
findsubset(reqdx,reqdy,k,sumx+v[i].first,sumy+v[i].second,v,temp,i+1);
}
int32_t main()
{
int Xa,Ya,Xb,Yb; // coordinates of X and Y
cin >> Xa >> Ya >> Xb >> Yb; // taking input
int k; // total no. of vectors
cin >> k;
vector<pair<int,int> > v(k);
for(int i=0;i<k;i++)
{
int x,y; // taking input of k vectors.
v.push_back({x,y});
}
int reqdx = (Xb-Xa); // Required sum of B-A in x-coordinate
int reqdy = (Yb-Ya); // Required sum of B-A in y-coordinate
vector<pair<int,int> > temp;
findsubset(reqdx,reqdy,k,0,0,v,temp,0);
}