Go Endgame
Time limit1sMemory limit128 MB
Given starting scores, region values, and sente flags, compute the final scores when Alice and Bob alternately pick regions and respond until all are settled.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Game theory, Greedy, Sorting
- Solved
- No attempts yet
Problem
Go is a board game played on a grid. The goal is to surround as much territory as possible with stones of your own color. Playing a full game of Go is extremely hard for computers, but the endgame — the stage where the borders of the territories are almost settled and the players only squeeze out the last few points — can be captured by a simplified model. This task studies such a simplified endgame model.
The game is played by Alice and Bob, who alternate turns. Alice currently has points of territory and Bob has points. There are separate regions, and a move inside one region never affects any other region. It is Alice's turn.
Play proceeds as follows. The player on turn chooses a region and plays in it; the opponent responds in the same region, and they keep responding to each other until that region is settled. Whoever is on turn afterward chooses the next region, and so on, until every region is settled.
The player who starts a region has an advantage there and usually gains more points. We model this per region: if Alice starts region she gains points and Bob gains nothing in it; if Bob starts region he gains points and Alice gains nothing in it.
A region may also be sente for a player. If a region is sente for the player who starts it, that same player is still on turn after the region is settled (so they also choose the next region). If the region is not sente for the player who starts it, the opponent is on turn once it is settled. A region can be sente for both players, for only one of them, or for neither.
Given the description of all regions, determine the final score assuming both players play optimally. Each player wants their own score to exceed the opponent's by as much as possible (or to trail by as little as possible); among lines of play that give the same score difference, each player prefers the one in which their own score is as large as possible.
Input
The input contains several instances, separated by single blank lines.
The first line of an instance contains three integers , , and (, , and ): Alice's current points, Bob's current points, and the number of unsettled regions.
Each of the next lines describes one region. The -th such line contains two integers and ( and ) and two characters and , separated by single spaces. Here and are the points Alice and Bob gain by starting that region. The character is S if the region is sente for Alice and G otherwise; likewise is S if the region is sente for Bob and G otherwise.
Process instances until the end of input.
Output
For each instance, print a single line with two integers and separated by one space: the final scores of Alice and Bob under optimal play, with Alice on turn at the start. Every instance satisfies .