@dietpepsi When I click the link to your blog, a page showed up asking for password to log in. Do you mind for telling me how to access your blog? I would like to see your explanation for this problem. Thanks!

This is my favorite part of this code, which can not only eliminates unnecessary exploration of grids that cannot be reached by any of previous buildings, but also avoid using boolean[][] visited array to record visited grids. Thanks a lot :)