Created
June 12, 2012 01:39
-
-
Save gabrielgreen/2913879 to your computer and use it in GitHub Desktop.
ladder depth first
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
void Main() | |
{ | |
//Store the root node in Container | |
//While (there are nodes in Container) | |
// N = Get the "next" node from Container | |
// Store all the children of N in Container | |
// Do some work on N | |
var paths = new List<int[]>(); | |
int N = 10; | |
var stack = new Stack(); | |
stack.Push(new int[] { 0 }); | |
while(stack.Count > 0) | |
{ | |
if(stack.Count > max) max = stack.Count; | |
var next = stack.Pop() as int[]; | |
int total = next.Sum(); | |
if(total < N + 1) | |
{ | |
stack.Push(next.Concat(new int [] { 1 }).ToArray()); | |
} | |
if(total < N + 2) | |
{ | |
stack.Push(next.Concat(new int [] { 2 }).ToArray()); | |
} | |
if(next.Sum() == N) | |
{ | |
paths.Add(next.ToArray()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment