There are four stacks. On the first stack there are n numbers 1, 2, ... n in random order. The other three stacks are empty. The goal is to determine, given the state of the first stack, whether is it possible to move all the elements to the last stack so that they are sorted. Allowed moves are moving the element from the first stack to the second or third stack, and from the second or third to the fourth (1>2, 1>3, 2>4, 3>4). Unlike towers of Hanoi, larger elements can sit atop smaller ones.
I'm supposed to write a program to do this, but I can't come up with an algorithm. Help please.