Created
July 7, 2014 16:36
-
-
Save satojkovic/91628a294fafaf51985c to your computer and use it in GitHub Desktop.
depth first search
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 <string> | |
int n = 4; | |
int k = 15; | |
int a[] = {1, 2, 4, 7}; | |
bool dfs(int i, int sum) | |
{ | |
printf("i == %d, sum == %d\n", i, sum); | |
if( i == n ) return sum == k; | |
if( dfs(i+1, sum) ) return true; | |
if( dfs(i+1, sum+a[i]) ) return true; | |
return false; | |
} | |
void solve() | |
{ | |
if( dfs(0, 0) ) printf("Yes\n"); | |
else { printf("No\n"); } | |
} | |
int main(int argc, char *argv[]) | |
{ | |
solve(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment