02. The Battle of the Five Armies
Loading...
+ Нов въпрос
arnold avatar arnold 36 Точки

The Battle of the Five Armies (C#)

Малко "съпорт" за тази задача. Стигнах до 90/100, но не мога да разбера от къде идва досадната грешка в тестовете.

https://pastebin.com/nNcEbEAv

 

https://judge.softuni.org/Contests/Practice/Index/3088#1

 

02. The Battle of the Five Armies

You can test your solutions in Judge.

The five armies refer to the Goblins, Wolves, Elves, Men and Dwarves join forces to fight off the orcs.

In order to do that, they have to go through the land and fight off the orcs. If they successfully reach Mordor, they will win the fight against the evil.

A standard map of the Middle World looks like this:

The Middle World

Legend

------M---
-------O--
--O-------
----------
-----A----

A è The Army, the player character

Oè Orcs, enemy

M è Mordor

- è Empty space

Each turn proceeds as follows:

  • First, Orcs spawn on the given index.
  • Then, the army moves in a direction, which decreases their armor by 1.
    • It can be “up”, “down”, “left”, “right
    • If the army tries to move outside of the field, they don't move but still has their energy decreased.
  • If an enemy is on the same cell where the army moves, the army fights him, which decreases their armor by 2. If the army’s armor drops at 0 or below, they die and you should mark this position with ‘X’.
  • If the army kills the enemy successfully, the enemy disappears.
  • If the army reaches the index where Mordor is, they win the war (disappear from the field), even if their armor is 0 or below.

Input

  • On the first line of input, you will receive ethe armor the army has.
  • On the second line of input, you will receive n – the number of rows the map of the Middle World will consist of.
    Range: [5-20]
  • On the next n lines, you will receive how each row looks.
  • Then, until the army dies, or reaches the throne, you will receive a move command and spawn row and column.

Output

  • If the army is runs out of armor, print "The army was defeated at {row};{col}."
  • If the army reaches Mordor, print "The army managed to free the Middle World! Armor left: {armor}"
  • Then, in all cases, print the final state of the map on the console.

Constraints

  • The map will always be rectangular.
  • The army will always run out of armor or reach Mordor.
  • There will be no case with spawn on invalid index.
  • There will be no case with two enemies on the same cell.
  • There will be no case with enemy spawning on the index where the army or Mordor is.

Examples

Input

Output

Comments

100

5

--M--

-----

-----

-----

--A--

down 3 0

up 3 1

up 3 2

up 3 3

The army managed to free the Middle World! Armor left: 96

-----

-----

-----

OOOO-

-----

Turn 1: An enemy spawns at [3;0], the army moves to [3;2], their armor decreases by 1.

Turn 2: An enemy spawns at [3;1], the army moves to [2;2], their armor decreases by 1.

Turn 3: An enemy spawns at [3;2], the army moves to [1;2], their energy decreases by 1.

Turn 4: An enemy spawns at [3;3], the army moves to [0;2], their energy decreases by 1, but they also move to the index where Mordor is – they win the war

 

3

5

--M--

-----

-----

-----

--A--

up 3 2

The army was defeated at 3;2.

--M--

-----

-----

--X--

-----

 

 

Тагове:
0
Module: C# Advanced 18/10/2021 19:32:02
Axiomatik avatar Axiomatik 1581 Точки
Best Answer
  • If the army tries to move outside of the field, they don't move but still has their energy decreased.

After underArmour--, army health needs to be checked even when not moving, since can trigger event of army dying without encountering an Ork unit.

 

Code refactor 100% =>

underArmour--;
 
                if (underArmour <= 0)
                    {
                        warMap[armyRow][armyCol] = 'X';
                        Console.WriteLine($"The army was defeated at {armyRow};{armyCol}.");
                        break;
                    }
 
                if (warMap[armyRow][armyCol] == 'O')
                {
                    underArmour -= 2;
 
                    if (underArmour <= 0)
                    {
                        warMap[armyRow][armyCol] = 'X';
                        Console.WriteLine($"The army was defeated at {armyRow};{armyCol}.");
                        break;
                    }
                }
 
                else if (warMap[armyRow][armyCol] == 'M')
                {
                    warMap[armyRow][armyCol] = '-';
                    Console.WriteLine($"The army managed to free the Middle World! Armor left: {underArmour}");
                    break;
                }

 

1
arnold avatar arnold 36 Точки

Thank you very much!

1