编程介的小学生 2020-01-03 23:49 采纳率: 0.4%
浏览 47

March of the Penguins 算法的计算

Problem Description
Somewhere near the south pole, a number of penguins are standing on a number of ice floes. Being social animals, the penguins would like to get together, all on the same floe. The penguins do not want to get wet, so they have use their limited jump distance to get together by jumping from piece to piece. However, temperatures have been high lately, and the floes are showing cracks, and they get damaged further by the force needed to jump to another floe. Fortunately the penguins are real experts on cracking ice floes, and know exactly how many times a penguin can jump off each floe before it disintegrates and disappears. Landing on an ice floe does not damage it. You have to help the penguins find all floes where they can meet.

A sample layout of ice floes with 3 penguins on them.

Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with the integer N (1 ≤ N ≤ 100) and a floating-point number D (0 ≤ D ≤ 100 000), denoting the number of ice pieces and the maximum distance a penguin can jump.

N lines, each line containing xi, yi, ni and mi, denoting for each ice piece its X and Y coordinate, the number of penguins on it and the maximum number of times a penguin can jump off this piece before it disappears (−10 000 ≤ xi, yi ≤ 10 000, 0 ≤ ni ≤ 10, 1 ≤ mi ≤ 200).

Output
Per testcase:

One line containing a space-separated list of 0-based indices of the pieces on which all penguins can meet. If no such piece exists, output a line with the single number −1.

Sample Input
2
5 3.5
1 1 1 1
2 3 0 1
3 5 1 1
5 1 1 1
5 4 0 1
3 1.1
-1 0 5 10
0 0 3 9
2 0 1 1

Sample Output
1 2 4
-1

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-26 21:53
    关注

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

    # 导入必要的库
    library(Rcpp)
    library(Rcpp::Rcpp)
    
    # 定义函数
    jump_distance <- function(x, y, d) {
      return(sqrt((x - y)^2 + d^2))
    }
    
    # 定义主函数
    march_of_the_penguins <- function() {
      
      # 获取测试数据的数量
      n_cases <- as.integer(read_line())
      
      # 遍历每个测试案例
      for(i in 1:n_cases){
        
        # 获取测试案例的数据
        N <- read_line()
        D <- as.numeric(read_line()) / 10000
        
        # 初始化结果列表
        result <- rep(NA, N)
        
        # 遍历每个冰块
        for(j in 1:N){
          
          # 获取当前冰块的信息
          x <- read_line()
          y <- read_line()
          n <- as.integer(read_line())
          m <- as.integer(read_line())
          
          # 计算当前冰块的距离
          dist <- jump_distance(x, y, D)
          
          # 检查是否满足条件
          if(m >= 0 && dist <= m){
            result[j] <- j
          }
        }
        
        # 输出结果
        print(paste("Case ", i+1, ": ", paste(result, collapse = " ")))
      }
    }
    
    # 调用主函数并输出结果
    march_of_the_penguins()
    

    这段代码使用了RcppRcpp::Rcpp来处理C++代码。首先导入必要的库,然后定义了一个jump_distance函数用于计算冰块之间的距离。接着定义了一个march_of_the_penguins函数,它接受一个整数作为输入(表示测试案例的数量),遍历每个测试案例,并计算每个冰块之间的最短距离。如果满足条件,则将对应的冰块编号添加到结果列表中。最后,调用march_of_the_penguins函数并输出结果。

    评论

报告相同问题?