编程介的小学生 2019-11-30 22:48 采纳率: 0.4%
浏览 88

The gold miners 程序的编写

Problem Description
There are some funny mini games on the internet. Sailormoon girls usually play these games in them spare time. They always play a mini game named THE GOLD MINNERS. Now they give you a problem about this game. Could you solve it? Now let’s look at the following picture.

We set the position of the minner as the origin of coordinate.There are many objects in this game,not only gold. They also have diamonds,stones,and other unknown things.All the objects can be treated as polygon. Every two polygons can not intersect. In this problem,we will give you some data about the angle in order. When the hook with certain angle sinking, it will always catch nearest object, and get the corresponding money. There will be nothing in the position of this object later. If there have no objects,the hook will return back to the origin of coordinate. How much money does the minner have at last?Do you know?

Input
There are multiple test cases.
Each case begins with a positive integer N (0 <= N <= 30) , it means there are N objects.
The following N * 2 lines describe the informatin about objects. Each object starts with a positive integer V (3 <= V <= 15), it means this object can be treated as a polygon with V vertex. Followed by a positive integer W which is the value of this object.
The next line contains V * 2 decimal number. X1,Y1,X2,Y2……Xv,Yv. You can suppose all coordinate data are in the double range.Vertex coordinates clockwise.
Then followed by a positive integer M (M <= 100) which is number of the minner cast hook.
The following M lines contains M positive integer.The Xth integer means the angle with which the minner cast hook the Xth.(0 < angle < 180)

Output
Calculate and print the value of the minner earn at last for each test case.

Sample Input
4
4 158
0.0 -1.0 2.0 -1.0 0.0 -3.0 -1.0 -2.0
4 37
4.0 -1.0 4.0 -2.0 3.0 -2.0 3.0 -1.0
6 360
2.0 -3.0 3.0 -3.0 2.0 -5.0 1.0 -5.0 1.0 -4.0 2.0 -4.0
5 223
-2.0 -4.0 -2.0 -5.0 -3.0 -6.0 -4.0 -5.0 -4.0 -4.0
3
45
16
127

5
4 10
-3.0 0.0 -2.0 -1.0 -3.0 -1.0 -4.0 0.0
4 208
-2.0 0.0 0.0 -1.0 3.0 -1.0 0.0 -3.0
6 660
3.0 -2.0 4.0 -2.0 5.0 -3.0 6.0 -3.0 6.0 -6.0 3.0 -6.0
4 25
-1.0 -4.0 2.0 -4.0 2.0 -6.0 -1.0 -6.0
3 18
0.0 -7.0 1.0 -8.0 -1.0 -8.0
3
34
64
90

Sample Output
418
251

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 06:19
    关注

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

    # 读取数据
    data <- readLines()
    
    # 解析输入数据
    n <- as.integer(strsplit(data[1], " ")[[1]][1])
    obj <- matrix(unlist(strsplit(data[n + 1], " ")[[1]]), nrow = n)
    angle <- as.integer(strsplit(data[n + 2], " ")[[1]][1]) - 1
    
    # 初始化变量
    minner_pos <- c(0, 0)
    money <- 0
    for (i in 1:n) {
      v <- obj[i, 1]
      w <- obj[i, 2]
      x <- obj[i, 3:v+3, ]
      y <- obj[i, v+3:end, ]
    
      # 将每个对象转化为多边形
      poly <- expand.grid(x, y)
    
      # 计算每个对象与最小距离
      d <- dist(poly[, 1:2], poly[, end-2:1], method = "euclidean")
      d[d == Inf] <- NA
      
      # 找到与最小距离最近的对象
      min_dist_obj <- which.min(d)
      min_dist <- d[min_dist_obj]
    
      # 如果没有物体,则返回
      if (!is.na(min_dist)) {
        # 把钩子放置在最近物体上
        minner_pos <- c(minner_pos, poly[min_dist_obj, ])
        money <- money + w
        
        # 移除最近物体及其周围所有物体
        for (j in 1:(end - start)) {
          x <- poly[j, 1]
          y <- poly[j, end-2:1]
          
          # 避免计算重复的点
          if (x != x[1]) {
            # 计算新位置
            new_x <- x[1] + (y[1] - x[1]) / diff(y)[1]
            new_y <- y[1] + (x[1] - y[1]) / diff(x)[1]
            
            # 将新位置添加回多边形
            poly <- rbind(poly, data.frame(x = new_x, y = new_y))
          }
        }
    
        # 检查是否还有未处理的物体
        if (any(is.na(d))) {
          # 只有最后一个物体未被处理,将其移动到原位
          minner_pos <- c(minner_pos, poly[-c(start:end), ])
          money <- money + w
        }
      }
    }
    
    # 输出结果
    print(money)
    
    评论

报告相同问题?