TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Fighters

Problem statement, definition, constraints, and public examples.

Problem Statement

In role-playing games, there are frequently battles between characters. Each character has a name. Each character has a number of hit points and is eliminated when his hit points are reduced to 0 or less. Each character has an amount of damage that he causes when he attacks an enemy -- this is the amount of hit points that the enemy loses when he is attacked by the character. A character loses hit points as the fight progresses, but, as long as he still has at least one hit point, his capacity to damage anyone he attacks stays constant.

An epic battle has begun. A number of characters have joined battle. They have positioned themselves in a circle. Each character always attacks the character on his right. As the battle goes on, the circle closes over the spots previously occupied by eliminated characters. The battle ends when there is at most one character surviving.

The attacks take place simultaneously. The characters are numbered 0, 1, ..., n-1 and arranged in a circle so that, initially, 0 attacks 1 while at the same instant 1 attacks 2, 2 attacks 3, and so on. Of course n-1 attacks 0.

Create a class Fighters that contains a method winner that is given a String[] name, a int[] hitPoints, and a int[] damage. There are exactly n elements in name, hitPoints, and damage, indexed by character number. The method simulates the battle and returns the name of the survivor. If no character survives, it returns "NOBODY" (in uppercase letters).

Definition

Class:
Fighters
Method:
winner
Parameters:
String[], int[], int[]
Returns:
String
Method signature:
String winner(String[] name, int[] hitPoints, int[] damage)
(be sure your method is public)

Constraints

  • name will contain between 1 and 50 elements inclusive
  • each element of name will contain between 1 and 15 uppercase letters 'A'-'Z'
  • hitPoints and damage will have the same number of elements as name
  • the elements of hitPoints and damage will be between 1 and 100 inclusive

Examples

  1. {"AL","ORK","OGRE"}
    {3,4,3}
    {1,2,1}
    Returns: "ORK"
    AL attacks ORK while ORK attacks OGRE and OGRE attacks AL. These attacks result in ORK being reduced to 3 hit points, OGRE being reduced to 1, and AL being reduced to 2. They repeat this pattern of attacks, resulting in ORK being reduced to 2 hit points, OGRE being reduced to -1 and thus being eliminated, and AL being reduced to 1. Now AL attacks ORK and ORK attacks AL (since OGRE has been eliminated). This eliminates AL, while ORK is reduced to 1 hit point -- ORK is the survivor.
  2. {"AL","ORK","OGRE"}
    {3,3,3}
    {1,2,1}
    Returns: "NOBODY"
    This is the same as the previous example, except that ORK starts with one less hit point, so at the end he has 0 hit points -- he is eliminated during the same attack that eliminated AL.
  3. {"HERBERT","HOWARD","THE","DUCK"}
    {5,6,7,8}
    {100,10,8,7}
    Returns: "NOBODY"
    All four characters are wiped out in the first attacks.
  4. {"BOB","BOB"}
    {10,3}
    {2,2}
    Returns: "BOB"
    Here we have two characters who happen to have the same name. Of course they attack each other anyway, and the first BOB wipes out the second BOB on their second attacks, leaving the first BOB as the survivor.
Back to all problems