The idea is inspired by #2 . Although original problem is technical, and we could/should solve it technically, its slowness also opens the door to demonstrate advantages of actually thoughtful design and algorithms. In this case sorting could be of initial help and the ultimate order of the screen exploration would be the NP complete https://en.wikipedia.org/wiki/Travelling_salesman_problem .