Epic Win!
Time limit1sMemory limit256 MB
Implement the given belief-state construction to print a rock-paper-scissors machine that beats a known opponent from any start state.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Simulation, Implementation
- Solved
- No attempts yet
Problem
In rock-paper-scissors two players show a move at the same time in every round: Rock, Paper, or Scissors. Equal moves are a draw. Otherwise Rock beats Scissors, Paper beats Rock, and Scissors beats Paper.
In this problem two finite state machines repeat this game forever. Formally, machine here means a Moore machine.
A machine that plays rock-paper-scissors has finitely many states. Each state fixes two things: the move shown in the coming round, and the state the machine moves to when its opponent shows Rock, Paper, or Scissors. Both machines learn the opponent's move only after the round is over.
You know the whole design of the opponent machine. One thing is missing: you do not know which state it starts in. Your machine still has to beat it in at least 99% of the first rounds. That is what we call an epic win.
Many machines reach that target, so this problem pins down one of them. Build your machine by the procedure below and print it.
Number the opponent states from 1 to . Write for the move shown in state , and for the opponent state reached from when you show .
Separation distance. For an ordered pair of opponent states define as follows. If then . Otherwise over the three moves . If no choice of your moves ever brings a round where the two states show different moves, then . Every pair with is .
Candidate sets. A candidate set is a nonempty set of opponent states. Reduce a candidate set by this rule: if for every two distinct , replace by the set holding only the smallest element of , and otherwise keep unchanged. One reduced candidate set is one state of your machine.
The move shown in a reduced set is this.
- If holds a single state, that is , show the move that beats .
- Otherwise take the pairs with , both in , and finite, and pick the smallest one comparing first, then , then . Going through Rock, Paper, Scissors in this order, show the first move that minimizes . Here counts as larger than every integer.
The transitions of are this. Let be the move chosen above and let be the observed opponent move. Put . If is empty, the transition on goes to state 1. Otherwise it goes to the reduced form of .
Number the states in this order. State 1 is the reduced form of the whole set . Handle the states from the smallest number upwards, and inside one state go through the observed moves in the order Rock, Paper, Scissors. Every time a set without a number shows up, give it the smallest free number. Equal sets always get the same number. Print the states in that order.
Input
The first line has one integer , the number of states of the opponent machine (). States are numbered from 1 to .
Each of the next lines describes one state. Line has a character and integers , , . The character is R, P, or S and is the move shown in state . The integers , , are the states the opponent moves to from state when you show Rock, Paper, or Scissors ().
Output
On the first line print , the number of states of your machine.
On each of the next lines print one state in the same format as the input: the move shown in that state as R, P, or S, then the states it moves to when the observed opponent move is Rock, Paper, or Scissors.
State 1 is the starting state of your machine. On every input of this problem the procedure above produces at most 50000 states.
Notes
Collect every opponent state that agrees with what you have observed through round , and you get the current candidate set of your machine. The two differ only where indistinguishable states were folded into one. So once the candidate set holds a single state you know every move the opponent will show and you win every remaining round.
In each round either the candidate set gets smaller or the separation distance of the chosen pair drops by one. That distance never exceeds and the set can shrink at most times, so fewer than rounds are not won. That is far under 1% of .