编程介的小学生 2019-11-22 23:28 采纳率: 0.2%
浏览 59

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-29 10:13
    关注

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

    # 导入必要的库
    library(Rcpp)
    library(Rcpp::Rcpp)
    
    # 定义函数
    jump_distance <- function(x, y, d) {
      return(sqrt((x - y)^2 + d^2))
    }
    
    # 定义主函数
    solve_penguins <- function(testcase_count) {
      # 初始化结果向量
      result <- rep(-1, testcase_count)
      
      # 遍历测试数据集
      for (i in 1:testcase_count) {
        # 获取当前测试数据的参数
        n <- readLines()[[i]][1]
        d <- as.numeric(readLines()[i][2])
        
        # 初始化penguins数组和ice_floes数组
        penguins <- matrix(NA, nrow = 1, ncol = 4)
        ice_floes <- matrix(NA, nrow = 1, ncol = 6)
        
        # 将冰块信息写入矩阵中
        for (j in 1:n) {
          x <- as.numeric(readLines()[i + j * n])
          y <- as.numeric(readLines()[i + j * n + 1])
          n_i <- as.integer(readLines()[i + j * n + 2])
          m_i <- as.integer(readLines()[i + j * n + 3])
          
          # 将冰块的信息添加到penguins和ice_floes矩阵中
          penguins[, c("X", "Y")] <- c(x, y)
          penguins[, c("ni", "mi")] <- c(n_i, m_i)
          
          # 计算每个冰块可以跳的距离
          for (k in 1:m_i) {
            ice_floes[j, k] <- jump_distance(penguins[k, 1], penguins[k, 2], d)
          }
        }
        
        # 对于每个冰块,检查它是否可以被所有penguins跳过
        for (j in 1:nrow(ice_floes)) {
          is_valid <- TRUE
          for (k in 1:ncol(ice_floes)) {
            if (ice_floes[j, k] <= 0 || ice_floes[j, k] > n_i) {
              is_valid <- FALSE
              break
            }
          }
          
          if (is_valid) {
            # 如果所有冰块都满足条件,输出当前测试数据的index
            result[i] <- j
          } else {
            # 如果没有满足条件的冰块,输出-1
            result[i] <- -1
          }
        }
      }
      
      # 返回结果向量
      return(result)
    }
    
    # 调用主函数并获取结果
    result <- solve_penguins(1L)
    print(result)
    

    请注意,上述代码使用了Rcpp库来实现C++风格的编程。在实际应用中,请确保已经安装了Rcpp包,并且正确配置环境变量以访问其中的函数。

    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题