-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path473.MatchstickstoSquare.cpp
46 lines (45 loc) · 1.13 KB
/
473.MatchstickstoSquare.cpp
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
class Solution {
public:
int a,b,c,d;
bool myfunc(vector<int> & match, int i){
if(i==match.size()){
if(a==0 and b==0 and c==0 and d==0)
return true;
return false;
}
if(match[i]<=a){
a-=match[i];
if(myfunc(match,i+1)) return true;
a+=match[i];
}
if(match[i]<=b){
b-=match[i];
if(myfunc(match,i+1)) return true;
b+=match[i];
}
if(match[i]<=c){
c-=match[i];
if(myfunc(match,i+1)) return true;
c+=match[i];
}
if(match[i]<=d){
d-=match[i];
if(myfunc(match,i+1)) return true;
d+=match[i];
}
return false;
}
bool makesquare(vector<int>& match) {
if(match.size()<4)
return false;
int sum=0;
for(int i:match)
sum+=i;
if( sum % 4 )
return false;
int length=sum/4;
a=length,b=length,c=length,d=length;
sort(match.rbegin(),match.rend());
return myfunc(match,0);
}
};