SRM 695, D2, 250-Pointer (BearNSWE)

James S. Plank

Thu Sep 1 22:37:33 EDT 2016
The picture for example 0 summarizes what you have to do here. First, you sum up all of the a[i], because that is the distance that the bear travels in the first M segments. In example 0, that sum is 7.

While you're doing that, you should maintain the (x,y) coordinate of the bear after each segment. You do that by adding or subtracting a[i] from the bear's x or y coordinate, depending on the value of dir[i]. Finally, you use the distance formula to calculate the bear's distance to the origin. Add the to the total and return the total.

In example 0, the Bear's final (x,y) coordinate is (3,-2), so his distance to the origin is sqrt( (-2)*(-2) + (3*3) ) = 3.6055512755, and his final distance is 10.6055512755.


My Solution.