Battle
SRM 126 · 2002-12-23 · by leadhyena_inran
Problem Statement
You are employed at a game studio, and are responsible for the balance of warrior powers in their latest RPG release. You decide to write a quick and dirty simulator that will take the warrior stats for a mock battle and determine the outcome, so you can get a feel for the numbers.
You will simulate as follows: You will be given two warrior
<WarriorName> <Health> <Defense> <Offense>
Each second (as soon as the battle starts), each warrior will strike the other warrior simultaneously, and each will cause the other damage, represented as an integer. This damage is equal to the <Offense> of the attacker minus the <Defense> of the defender, if positive(if negative or zero then there is no damage). The new <Health> of the warrior being attacked will be decremented this damage. A warrior is defeated when the warrior's <Health> is less than or equal to 0.
Example: Let warrior1 be "Rynos 100 20 30" and warrior2 be "Unka-Tarkal 1000 7 50". In the first second, Rynos attacks the Unka-Tarkal doing 30-7 or 23 damage bringing the Unka-Tarkals health to 1000-23 or 977. Simultaneously, the Unka-Tarkal will retaliate doing 50-20 or 30 damage to Rynos, putting him at 70 health. You would repeat this until the 4th second, where Rynos is defeated by the Unka-Tarkal. You would return the warrior defeated followed by the number of seconds it took, like this: Rynos 4. Note that there is a limit to seconds: If a battle takes longer than 1000 seconds, the battle is called off and you will return "NONE 1000" to symbolize this. Also, if the two warriors take each other out simultaneously, return "NONE " followed by the number of seconds it took to reach a draw.
You will write a function firstDefeated that takes warrior1 and warrior2, and returns the result
Notes
- If a warrior's attack is less than the defender's defense, that defender takes no damage.
Constraints
- Both warrior1 and warrior2 will be of the form (quotes and angle brackets for clarity only): "
" - The warriors will not have identical names.
- No integers in the warrior Strings will contain extra leading zeros.
will only consist of lowercase and uppercase letters ('a'-'z','A'-'Z') and will be between 1 and 20 characters in length, inclusive. will be an integer between 1 and 1000000, inclusive. will be an integer between 0 and 1000, inclusive. will be an integer between 0 and 1000, inclusive.
"Rynos 100 20 30" "UnkaTarkal 1000 7 50" Returns: "Rynos 4"
From the example above.
"UnkaTarkalA 1000 7 50" "UnkaTarkalB 1000 7 50" Returns: "NONE 24"
Matched against one another, the two UnkaTarkals defeat one another in 24 rounds, with no clear winner.
"Murp 1000000 1000 0" "Rynos 100 20 30" Returns: "NONE 1000"
Rynos and the Murp are at a stalemate, and neither will defeat one another in under 1000 turns.
"AntA 1001 0 1" "AntB 1000 0 1" Returns: "AntB 1000"
AntB is defeated in the 1000th second, just on the boundary of having the match called for time.
"AntA 1001 0 1" "AntB 1002 0 1" Returns: "NONE 1000"
Even though AntB will win in 1001 seconds, the match is called off at 1000 seconds.
Submissions are judged against all 34 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Battle with a public method string firstDefeated(string warrior1, string warrior2) · 34 test cases · 2 s / 256 MB per case