思路
既然給出了每個地毯的座標和長度,在地毯數目不是特別多的情況下,我們可以用一個結構體來存儲所有地毯的信息。那麼計算的時候從0開始枚舉,如果這個地方有一張地毯在上面,就讓答案等於它就好了。注意因爲要求的是最後一張地毯是哪個,所以我們判斷的範圍應該一步一步縮小,保證求出來的是最終答案。
代碼
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
int nextInt()
{
int num = 0;
char c;
bool flag = false;
while ((c = std::getchar()) == ' ' || c == '\r' || c == '\t' || c == '\n');
if (c == '-')
flag = true;
else
num = c - 48;
while (std::isdigit(c = std::getchar()))
num = num * 10 + c - 48;
return (flag ? -1 : 1) * num;
}
int n, x, y, ans = -1;
struct dt
{
int x, y, d1, d2;
} all[100123];
int main()
{
n = nextInt();
for (int i = 1; i <= n; i++)
{
all[i].x = nextInt();
all[i].y = nextInt();
all[i].d1 = nextInt();
all[i].d2 = nextInt();
}
x = nextInt();
y = nextInt();
for(int i = 1; i <= n; i++)
{
int tmpx = all[i].x + all[i].d1;
int tmpy = all[i].y + all[i].d2;
if(x <= tmpx && x >= all[i].x && y <= tmpy && y >= all[i].y)
ans = i;
}
std::cout << ans << std::endl;
#ifdef __EDWARD_EDIT
std::cin.get();
std::cin.get();
#endif
return 0;
}