Path Finder in Prolog Blog post explains the problem and solution: https://www.asjadk.io/path-finding-in/