解题思路:1.记录每张地毯覆盖的范围,范围表示为:记录地毯左下角的横纵坐标以及地毯右上角的横纵坐标,大于等于地毯左下角横纵坐标以及小于地毯右上角横纵坐标的点都被该张地毯覆盖。(注:地毯右上角坐标即为地毯左下角横坐标加上地毯在x轴方向,纵坐标加上地毯在y轴方向的长度)
2.拿到所求地面点坐标时,先从最上面一层开始找起,若找到该点被地毯覆盖,则返回该地毯编号,并停止遍历。若遍历到最下面一层依旧没有,则返回-1。
import java.util.Scanner;
public class Main {
public class Position {
public int e;// 表示是第几层地毯
public int x;// 表示左下角横坐标
public int y;//表示左下角纵坐标
public int x1;// 表示右上角横坐标
public int y1;//表示右上角纵坐标
public Position(int e, int x, int y, int x1, int y1) {
this.e = e;
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
}
public Position() {
this.e = 0;
this.x = 0;
this.y = 0;
this.x1 = 0;
this.y1 = 0;
}
}
private Position[] cover;
private int N;
public Main(int N) {
this.N = N;
cover = new Position[N];
}
public void add(int e, int x, int y, int x1, int y1) {
//把每层地毯编号,左下角以及右上角横纵坐标放入数组中
if (cover[e] == null) {
cover[e] = new Position(e, x, y, x1, y1);
}
}
//从最上面一层即编号为n的地毯开始找范围有包括该坐标的地毯
public int method(int n, int x, int y) {
for (int i = n; i > 0; i--) {
if (cover[i].x <= x && cover[i].x1 >= x) {
if (cover[i].y <= y && cover[i].y1 >= y) {
return i;
}
}
}
return -1;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
Main arr=new Main(n+1);
for(int i=1;i<=n;i++) {
int a=in.nextInt();
int b=in.nextInt();
int g=in.nextInt();
int k=in.nextInt();
arr.add(i, a, b, a+g, b+k);
}
int x=in.nextInt();
int y=in.nextInt();
int s=arr.method(n, x, y);
System.out.println(s);
}
}