Last active
August 29, 2015 14:16
-
-
Save balamark/cf4675513ba2d967fae1 to your computer and use it in GitHub Desktop.
variation of coin change DP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <numeric> | |
#include <algorithm> | |
#include <cstring> | |
using namespace std; | |
int T, m, S, S2; | |
int ctype[40][2]; | |
int memo[300][300]; | |
//smallest amount of e-coins needed | |
int way(int a, int b){ | |
if((a*a+b*b)>S2) return numeric_limits<int>::max(); | |
if((a*a+b*b)==S2) return 0; | |
if(memo[a][b]!=-1) return memo[a][b]; | |
int w=numeric_limits<int>::max(); | |
for(int i=0;i<m;++i) w = min(w, way(a+ctype[i][0], b+ctype[i][1])); | |
return memo[a][b] = (w==numeric_limits<int>::max()?numeric_limits<int>::max():1+w); | |
} | |
int main(){ | |
scanf("%d", &T); | |
for(int i=0;i<T;++i){ | |
scanf("%d %d", &m, &S); S2=S*S; | |
for(int j=0;j<m;++j){ | |
scanf("%d %d", &ctype[j][0], &ctype[j][1]); | |
} | |
memset(memo, -1, sizeof memo); | |
int ans=way(0, 0); | |
if(ans==numeric_limits<int>::max()) printf("not possible\n"); | |
else printf("%d\n", ans); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
passing only one parameter "value" as sum of current chosen coins is not enough since (3,0) and (4,0) can have square sum of 25 but it's not a valid answer.
Thus, we have to track both conventional and InfoTechnological values.
p.s. To differentiate the "not possible" case, we set the minimum answer to INT_MAX. And if after backtracking, the best we got is still INT_MAX, we concluded that there is no way to reach the desired value.