Editorial for Baltic OI '08 P1 - Game
Submitting an official solution before solving the problem yourself is a bannable offence.
The problem is a typical game theory problem. Let us denote by the distance between 's and 's starting points. Both players have the same distance to travel. Therefore, it is obvious that both players should move along a shortest path between starting points. Otherwise, a player would make at least moves and his opponent would win. Since player moves first, he will win if and only if player is unable to reach the same square as player in moves. Hence, if is odd, then wins. So, from now on, we assume that .
Since players move along shortest paths, it is easy to find for each player, all such squares that can be reached in exactly moves. We can do this by running the BFS algorithm twice and finding the distances from 's and 's starting points to all the squares. After moves, player can be on one of the squares whose distance to 's starting point is and the distance to 's starting point is (and conversely for player ).
This basic observation is necessary to solve this problem correctly, as it gives us the order in which all configurations of 's and 's positions should be processed.
Model Solution
This solution uses simple dynamic programming. Let and be the lists of such squares which can be reached by players and , respectively, in moves, and can be reached by their respective opponent in moves. By and , we will denote the elements of these lists. Let be true if after moves, player has a winning strategy when his piece is on the square and player is on the square . If has a winning strategy, then is false. Please note that equals , and is true for all and such that (that is, the pieces cannot stand on the same square).
Please note that has a winning strategy, iff he can make such a move, after which can only make moves, after which still has a winning strategy. More formally, let be the list of squares from onto which player can move from . Let be a similar list for player . If for some , for all the value of is true, then is true, otherwise it is false.
Using the above observation, we can calculate values for . Player has a winning strategy in the whole game if and only if is true.
The only problem is to compute and lists efficiently. For square where one of the players can be after moves, we can easily find all squares where this player can be after moves. The problem is to find the position of on the and lists. But since each square is present on at most two such lists, when we add some square to some list, we can also store its position on an appropriate list.
Since each value can be computed in constant time, the total time complexity is . Please note that although there are values , we only have to store for two consecutive values of . Moreover, since each square belongs to at most two lists /, these lists occupy memory. Therefore, the overall memory complexity is .
Comments