Optimized precomputing paths to goal using BFS