洛谷 P1003 铺地毯

题目:https://www.luogu.org/problem/P1003


 

暴力做法就是开一个场地大小的数组,模拟铺地毯的过程,但是数据太大,没法开这么大的数组。观察发现,只要存下左下角坐标和长宽就行,从后往前遍历,遇到(x,y)处铺有地毯就break,就找到答案。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 const int MAXN = 10000 + 10;
 5 int cnt[MAXN][4], id;
 6 
 7 int main()
 8 {
 9     int n;
10     cin >> n;
11     for (int i = 0; i < n; i ++ )
12     {
13         int a, b, g, k;
14         cin >> cnt[i][0] >> cnt[i][1] >> cnt[i][2] >> cnt[i][3]; 
15     }
16     int x, y;
17     cin >> x >> y;
18     int i;
19     for (i = n - 1; i >= 0; i -- )
20     {
21         if ((x >= cnt[i][0] && x <= cnt[i][0] + cnt[i][2]) && (y >= cnt[i][1] && y <= cnt[i][1] + cnt[i][3]))
22         {
23             printf("%d",i + 1);
24             break;
25         }
26     }
27     if (i < 0)  cout << "-1";
28     return 0;
29 }
View Code

 

posted @ 2019-09-13 14:43  chuyds  阅读(153)  评论(0编辑  收藏  举报