思路
-
存储地毯信息:使用数组存储每张地毯的四个参数(左下角坐标和长宽)
-
逆序检查:从最后一张地毯开始向前检查,因为后铺的地毯在上面
-
判断点是否在地毯内:对于点(x,y),检查是否满足:
-
a <= x <= a + g
-
b <= y <= b + k
-
-
找到即输出:一旦找到包含该点的地毯,输出其编号(编号=下标+1)
-
未找到输出-1:如果所有地毯都不包含该点,输出-1
代码实现
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> carpets(n, vector<int>(4));
for (int i = 0; i < n; i++) {
cin >> carpets[i][0] >> carpets[i][1] >> carpets[i][2] >> carpets[i][3];
}
int x, y;
cin >> x >> y;
int ans = -1;
for (int i = n - 1; i >= 0; i--) {
int a = carpets[i][0], b = carpets[i][1];
int g = carpets[i][2], k = carpets[i][3];
if (x >= a && x <= a + g && y >= b && y <= b + k) {
ans = i + 1;
break;
}
}
cout << ans << endl;
return 0;
}