Sounds like a problem where PROLOG could shine...
As far as I remember writing the data as predicate will be all you'll have to do, to let Prolog "find" the solution(s)
thanx to unification and backtracking...
IMHO, There are really few problems where PROLOG is the right tool, this one is one of them...
(Maybe
those modules could help)
"
Only
Bad
Coders
Code
Badly
In
Perl" (OBC2BIP)