Fighting for Triangles
Time limit2sMemory limit512 MB
On a triangular board with some edges already drawn, two players alternate adding edges, claiming a unit triangle when their edge completes it. Decide the winner with optimal play.
- Level
Medium7 of 10
- Topics
- Game theory, Graph, Simulation, Backtracking
- Solved
- No attempts yet
Problem
Andy and Ralph are playing a two-player game on a triangular board like the one below.

On each turn, a player must choose two adjacent vertices and draw the line segment (edge) that connects them. If the newly drawn edge completes a triangle on the board — only the smallest unit triangles count — the player claims that triangle and immediately draws another edge. Otherwise the turn ends and the other player moves. Each player tries to claim as many triangles as possible. Andy always moves first.
For example, suppose it is Andy's turn and the board already has the five edges shown below. If Andy draws edge , he completes the triangle formed by edges , , and , claims it, and keeps playing.

Given a board that already has some edges drawn on it, determine the winner assuming both players play optimally. Note that if a triangle is already present on the board before the first move, neither player claims it.
Input
The input consists of several test cases. Each test case begins with a line containing an integer (), the number of edges already drawn on the board before the game begins. The next line contains integers, the indices of those edges. The input ends with a line containing .
Output
For each test case, print a single line with the result of the game. Print Andy wins if Andy claims more triangles, Ralph wins if Ralph claims more, or Draw if both players claim the same number of triangles.