Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class temp_MyTemp {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int wave = Integer.parseInt(scanner.nextLine());
- ArrayDeque<Integer> defensePlatQueue = new ArrayDeque<>();
- int[] defense = Arrays.stream(scanner.nextLine().split("\\s+"))
- .mapToInt(Integer::parseInt)
- .toArray();
- for (int i = 0; i < defense.length; i++) {
- defensePlatQueue.offer(defense[i]);
- }
- int addBonusDefense = 0;
- // Map<Integer, ArrayDeque<Integer>> attackArmyMap = new LinkedHashMap<>();
- ArrayDeque<Integer> attackArmyStack = null;
- for (int i = 1; i <= wave; i++) {
- String line = scanner.nextLine();
- if (i % 3 == 0) {
- addBonusDefense = Integer.parseInt(scanner.nextLine());
- defensePlatQueue.offer(addBonusDefense);
- }
- int[] atackPower = Arrays.stream(line.split("\\s+"))
- .mapToInt(Integer::parseInt)
- .toArray();
- attackArmyStack = new ArrayDeque<>();
- for (int j = 0; j < atackPower.length; j++) {
- attackArmyStack.push(atackPower[j]);
- }
- int sizeAttack = attackArmyStack.size();
- for (int j = 0; j < sizeAttack; j++) {
- int hit = 0;
- int attack = attackArmyStack.peek();
- int def = defensePlatQueue.peek();
- hit = def - attack;
- if (hit < 0) {
- defensePlatQueue.poll();
- attackArmyStack.pop();
- attackArmyStack.addFirst(Math.abs(hit));
- if (defensePlatQueue.isEmpty()) {
- break;
- }
- j--;
- } else {
- if (hit == 0) {
- defensePlatQueue.poll();
- if (defensePlatQueue.isEmpty()) {
- attackArmyStack.pop();
- break;
- }
- } else {
- defensePlatQueue.poll();
- defensePlatQueue.addFirst(hit);
- }
- attackArmyStack.pop();
- }
- }
- if (defensePlatQueue.isEmpty()) {
- break;
- }
- if (attackArmyStack.isEmpty() && i == wave) {
- break;
- }
- }
- if (defensePlatQueue.isEmpty()) {
- System.out.println("The Trojans successfully destroyed the Spartan defense.");
- System.out.print("Warriors left: ");
- System.out.println(attackArmyStack.toString().replaceAll("[\\[\\]]", ""));
- } else {
- System.out.println("The Spartans successfully repulsed the Trojan attack.");
- System.out.print("Plates left: ");
- System.out.println(defensePlatQueue.toString().replaceAll("[\\[\\]]", ""));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement