Skip to content

Instantly share code, notes, and snippets.

@satojkovic
Created July 7, 2014 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satojkovic/91628a294fafaf51985c to your computer and use it in GitHub Desktop.
Save satojkovic/91628a294fafaf51985c to your computer and use it in GitHub Desktop.
depth first search
#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