# n d sx sy tx ty answer Comments ------------------------------------------------------------------------------------ 0 100 10 0 0 3 4 2 Jump to (99,99) and then to (4,2) 1 100 5 0 0 3 4 1 sqrt(4*4+3*3) = 5 2 100 200 0 0 3 4 -1 Can't jump anywhere from 0,0 3 10 7 4 4 5 5 3 4 1 1 0 0 0 0 0
If you don't go directly from start to finish, there is a shortest path that only involves nodes on the edges of the square. |
Now you only have 4000 potential nodes, and your BFS can complete in time. If you're presenting this problem in CS494, I want you to prove the realization (and of course illustrate it very nicely graphically). That's how I solved it when I did the problem for topcoder. I constructed the graph on the fly, while I did the BFS.
I'll also state that if you're solving this in a programming competition like Topcoder, stop here and don't think about it any more. The thinking is more likely to get you into trouble, and this solution is fast enough.
However, if you're presenting this for CS494/594, read on:
If s or t contain more than two corner points, you'll have to do those calculations separately for each unique pair of corner points in s and t.