Skip to Content
CoursesCSC102จำลองเกมแพล็นส์ วีเอส. ซอมบีส์

จำลองเกมแพล็นส์ วีเอส. ซอมบีส์

โจทย์

In the game Plants vs. Zombies, a single row of plants with varying attack power defends against a wave of zombies, each with a specific health and walking speed. All zombies start at the same initial distance from the defense line. Each plant has a delay between attacks, and your task is to simulate the battle as the zombies attempt to cross the defense line.

You are given the following:

  • An array plants where plants[i] represents the attack power of the plant at position i.
  • An array attackDelay where attackDelay[i] represents the delay in seconds between attacks for the plant at position i.
  • An array zombies where zombies[j] represents the health of the zombie at position j.
  • An array speed where speed[j] represents the walking speed (units/second) of zombie j.
  • An integer initialDistance which is the same initial distance (in units) for all zombies from the defense line.
  • An array targets where targets[j] indicates the index of the plant that attacks zombie j.

The simulation proceeds as follows:

  • Each zombie moves toward the defense line at its walking speed. The total time before a zombie reaches the defense line is timeToReach[j] = initialDistance / speed[j].
  • A plant can attack a zombie at most Math.floor(timeToReach[j] / attackDelay[targets[j]]) times before the zombie crosses the defense line.
  • Each plant attack reduces the zombie’s health by the plant’s attack power. If a zombie’s health becomes zero or negative before reaching the defense line, it is considered defeated. Otherwise, it successfully crosses the defense line.

Your task:

  • Determine the final health of each zombie after the battle (e.g., when zombie reaches the defense line or die before reach the line).
  • Count the total number of zombies defeated.

Input Format:

  • The first line contains two integers n (number of plants) and m (number of zombies).
  • The second line contains n integers representing the attack power of the plants.
  • The third line contains n integers representing the attack delay of the plants (in seconds).
  • The fourth line contains m integers representing the initial health of the zombies.
  • The fifth line contains m integers representing the walking speed of the zombies (in units/second).
  • The sixth line contains an integer initialDistance (same for all zombies, in units).
  • The seventh line contains m integers, where targets[j] specifies the index of the plant that attacks zombie j.

Output Format:

  • An array representing the final health of each zombie after the battle.
  • An integer representing the total number of zombies defeated.

Additional Details:

  • Floating-point division should be handled carefully for the calculation of walking time and maximum attacks. Use Math.floor() to compute maximum attack rounds.

Sample Input and Output with Explanation:

Write a Java program that simulates this enhanced Plants vs Zombies battle, accounting for attack delays and zombie walking speed, and outputs the final health of each zombie and the number of zombies defeated.

โค้ด

import java.util.*; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numOfPlants = scanner.nextInt(); int numOfZombies = scanner.nextInt(); int[] plantsAttackPower = new int[numOfPlants]; int[] plantsAttackDelay = new int[numOfPlants]; for (int i = 0; i < numOfPlants; i++) plantsAttackPower[i] = scanner.nextInt(); for (int i = 0; i < numOfPlants; i++) plantsAttackDelay[i] = scanner.nextInt(); int[] zombiesHealth = new int[numOfZombies]; int[] zombiesWalkingSpeed = new int[numOfZombies]; for (int i = 0; i < numOfZombies; i++) zombiesHealth[i] = scanner.nextInt(); for (int i = 0; i < numOfZombies; i++) zombiesWalkingSpeed[i] = scanner.nextInt(); int zombiesInitialDistance = scanner.nextInt(); int zombieDefeatedCount = 0; for (int i = 0; i < numOfZombies; i++) { int plantIndex = scanner.nextInt(); int timeToReach = zombiesInitialDistance / zombiesWalkingSpeed[i]; int maximumAttack = (int) Math.floor((double) timeToReach / plantsAttackDelay[plantIndex]); for (int j = 0; j < maximumAttack; j++) { zombiesHealth[i] = Math.max(0, zombiesHealth[i] - plantsAttackPower[plantIndex]); if (zombiesHealth[i] <= 0) { zombieDefeatedCount++; break; } } } for (int health : zombiesHealth) System.out.print(health + " "); System.out.println(); System.out.print(zombieDefeatedCount); } }

คำอธิบาย

ปรับปรุงล่าสุด