Hello! In this post, we will solve a coding test problem using the C# language. The topic is ‘Jumong’s Command’. This problem simulates the situation where Jumong issues commands and implements an algorithm to handle them efficiently. I will explain in detail how to solve the problem and implement the code.
Problem Description
Jumong gives commands to warriors before each battle. Each warrior performs their duties according to the commands they receive. The warriors interpret Jumong’s commands as follows:
- Warrior’s unique number (a positive integer starting from 1)
- Content of the command (indicates what action needs to be performed)
Jumong has N warriors and M commands. The commands are given in the following format:
1. ATTACK A B // Warrior A attacks warrior B 2. DEFENSE A // Warrior A assumes a defensive posture 3. RETREAT A // Warrior A retreats
If a warrior successfully performs their duty, the result should output ‘SUCCESS’, and if they fail, it should output ‘FAIL’. Warriors do not perform commands if they lose interest before executing the command. Interest decreases over time. If a warrior is in a situation where they cannot perform the command, ‘FAIL’ should be printed.
Input Format
The input is given in the following format:
N (number of warriors) M (number of commands) Interest decay rate (0 ~ 1) Commands (M commands)
Output Format
The results are printed for each command. The results are listed in the same order as the commands.
Approach to the Problem
This problem requires an algorithm to process the given commands and determine whether each warrior can perform the commands. To solve the problem, I will consider the following steps:
- Create a data structure to store the warriors and command lists.
- Assume that each warrior initially has 100% interest.
- Decrease the warrior’s interest according to the given interest decay rate, and process each command while updating their status.
- Record success or failure based on the results of the commands.
Implementation
Code Example
The following is a C# implementation based on the above approach:
using System;
using System.Collections.Generic;
namespace JumongCommand
{
class Warrior
{
public int Id { get; set; }
public double Interest { get; set; }
public Warrior(int id)
{
Id = id;
Interest = 1.0; // 100% interest
}
}
class Program
{
static void Main(string[] args)
{
int N = int.Parse(Console.ReadLine());
int M = int.Parse(Console.ReadLine());
double interestDecayRate = double.Parse(Console.ReadLine());
List warriors = new List();
for (int i = 1; i <= N; i++)
{
warriors.Add(new Warrior(i));
}
List results = new List();
for (int i = 0; i < M; i++)
{
string command = Console.ReadLine();
string[] parts = command.Split(' ');
if (parts[0] == "ATTACK")
{
int attackerId = int.Parse(parts[1]);
int targetId = int.Parse(parts[2]);
ProcessAttack(warriors, results, attackerId, targetId);
}
else if (parts[0] == "DEFENSE")
{
int defenderId = int.Parse(parts[1]);
ProcessDefense(warriors, results, defenderId);
}
else if (parts[0] == "RETREAT")
{
int retreatId = int.Parse(parts[1]);
ProcessRetreat(warriors, results, retreatId);
}
// Apply interest decay
foreach (var warrior in warriors)
{
warrior.Interest -= interestDecayRate;
if (warrior.Interest < 0)
warrior.Interest = 0;
}
}
foreach (var result in results)
{
Console.WriteLine(result);
}
}
static void ProcessAttack(List warriors, List results, int attackerId, int targetId)
{
var attacker = warriors[attackerId - 1];
var target = warriors[targetId - 1];
if (attacker.Interest > 0)
{
results.Add("SUCCESS");
}
else
{
results.Add("FAIL");
}
}
static void ProcessDefense(List warriors, List results, int defenderId)
{
var defender = warriors[defenderId - 1];
if (defender.Interest > 0)
{
results.Add("SUCCESS");
}
else
{
results.Add("FAIL");
}
}
static void ProcessRetreat(List warriors, List results, int retreatId)
{
var retreatingWarrior = warriors[retreatId - 1];
if (retreatingWarrior.Interest > 0)
{
results.Add("SUCCESS");
}
else
{
results.Add("FAIL");
}
}
}
}
Code Explanation
The code defines a ‘Warrior’ class to manage the state of each warrior. Each warrior has a unique ID and a current interest as properties. The main program reads the warrior list and command list in order and calls separate methods to process each command, updating the results.
Verification of Results
After the commands are completed, the result list is printed to confirm the final success or failure results. This is a simple implementation of how warriors execute commands and manage resources.
Test Cases
We can create several test cases to check if it works properly.
Example Input
5 3 0.1 ATTACK 1 2 DEFENSE 3 RETREAT 4
Expected Results
SUCCESS SUCCESS SUCCESS
By testing various inputs in this manner, we can check if the algorithm correctly responds to all situations.
Conclusion
In this post, we explored the process of solving the ‘Jumong’s Command’ problem. Understanding the structure and implementation of the algorithm, as well as building the logic for processing various commands, was important. I hope you continue to improve your skills through more algorithm problems. Thank you!