编程介的小学生 2019-12-23 21:41 采纳率: 0.4%
浏览 54

Fruit Ninja 怎么来写的

Problem Description

Fruit Ninja is a juicy action game enjoyed by millions of players around the world, with squishy, splat and satisfying fruit carnage! Become the ultimate bringer of sweet, tasty destruction with every slash.
--- wikipedia

It is a very popular game on cell phones where people can enjoy cutting the fruit by touching the screen. The screen is rectangular, and all the fruit can be considered as circles, with coordinate of the center, and radius. Note that the fruit may overlap with each other. In this problem, a touch is a straight line cutting through the whole screen, scoring all the fruits it cuts or touches.
Now Fred is playing the Fruit Ninja, and seems absorbed in the game. He's desperate to create a new record, so he asks you for help. Now you are given a screen shot of the game, help him find the highest score he can get in a single touch.

Input
The first line contains an integer T(1 <= T <= 50), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 1000), indicating the number of fruit.
The following N lines each contains three integers Xi,Yi,Ri(-1000 <= X,Y <= 1000,1 <= Ri <= 1000), representing a fruit on the screen, where (X,Y ) is the coordinate of the center of the fruit, and Ri is the radius.
You can assume the screen is infinite.

Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is maximum number of fruit that you can cut in a single touch.

Sample Input
2
4
-2 5 1
5 5 1
-3 2 1
0 1 1
4
-4 5 1
3 2 1
-5 3 1
4 -3 1

Sample Output
Case #1: 3
Case #2: 2

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-28 01:15
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # Import necessary packages
    library(readline)
    
    # Read input from user
    t <- readline(prompt = "Enter the number of test cases: ")
    
    for (i in 1:t) {
      n <- readInt(prompt = paste("Number of fruits:", i))
      
      # Initialize variables
      maxScore <- 0
      
      # Iterate over each fruit
      for (j in 1:n) {
        x <- readDouble(prompt = paste("X coordinate of fruit", j, ": "))
        y <- readDouble(prompt = paste("Y coordinate of fruit", j, ": "))
        r <- readDouble(prompt = paste("Radius of fruit", j, ": "))
        
        # Calculate distance between the player and the fruit
        dist <- sqrt((x - player.x)^2 + (y - player.y)^2)
        
        # Check if the fruit is within range
        if (dist <= r) {
          maxScore <- max(maxScore, dist)
        }
      }
      
      # Print the result for the current test case
      cat(paste("Case #", i, ": ", maxScore), "\n")
    }
    

    This code reads in the number of test cases t from the user, then iterates over each test case and prompts the user for the coordinates of each fruit. It calculates the distance between the player (player) and each fruit, and checks if the fruit is within range. If the fruit is within range, it updates the maximum score accordingly. Finally, it prints out the maximum score for the current test case.

    评论

报告相同问题?