铺地毯

var
 a,b,g,k:array[1..10000]of longint;
 i,n,j,x,y:longint;
begin
 read(n);
 for i:=1 to n do
  readln(a[i],b[i],g[i],k[i]);
 read(x,y);
 for i:=n downto 1do
  if (x>=a[i])and(y>=b[i])and(x<=a[i]+g[i])and(y<=b[i]+k[i]) then
  begin
   write(i);
   halt;
  end;
 write('-1');
end.
### Java 实现铺地毯算法 为了实现在给定位置查找最顶层地毯的功能,可以采用如下方法: 在Java中处理此问题时,首先定义一个类来保存每张地毯的信息。这可以通过创建一个`Carpet`类完成,其中包含地毯左下角的横纵坐标以及宽度和高度。 ```java class Carpet { int x; int y; int width; int height; public Carpet(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } } ``` 接着,在主函数或者特定的方法里读入输入数据并初始化地毯对象数组。对于每一个查询点(x, y),逆序遍历地毯列表以找到最先满足条件即覆盖该点的最后一张地毯,并返回其索引加一作为结果;如果没有任何一张地毯能覆盖这个点,则输出-1[^2]。 下面是完整的Java代码实现: ```java import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { static class Carpet { int x; int y; int width; int height; public Carpet(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Carpet> carpets = new ArrayList<>(); int n = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < n; ++i) { String[] parts = scanner.nextLine().split(" "); carpets.add(new Carpet( Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) )); } String[] queryPointParts = scanner.nextLine().split(" "); int qx = Integer.parseInt(queryPointParts[0]); int qy = Integer.parseInt(queryPointParts[1]); boolean found = false; for (int i = n - 1; i >= 0 && !found; --i) { // 反向迭代寻找最上层的地毯 Carpet carpet = carpets.get(i); if ( qx >= carpet.x && qx <= carpet.x + carpet.width && qy >= carpet.y && qy <= carpet.y + carpet.height ) { System.out.println(i + 1); // 输出地毯编号(从1开始) found = true; } } if (!found) { System.out.println(-1); // 若无任何地毯覆盖则打印-1 } } } ``` 上述代码实现了对多个矩形地毯按顺序铺设后的查询功能,能够有效地找出指定坐标的最上方一层地毯编号[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值