UVa - 1312 - Cricket Field
Once upon a time there was a greedy King who ordered his chief Architect to build a field for royal cricket inside his park. The King was so greedy, that he would not listen to his Architect's proposals to build a field right in the park center with pleasant patterns of trees specially planted around and beautiful walks inside tree alleys for spectators. Instead, he ordered neither to cut nor to plant even a single tree in his park, but demanded to build the largest possible cricket field for his pleasure. If the Kind finds that the Architect has dared to touch even a single tree in his park or designed a smaller field that it was possible, then the Architect will loose his head. Moreover, he demanded his Architect to introduce at once a plan of the field with its exact location and size.
Your task is to help poor Architect to save his head, by writing a program that will find the maximum possible size of the cricket field and its location inside the park to satisfy King's requirements.
The task is somewhat simplified by the fact, that King's park has a rectangular shape and is situated on a flat ground. Moreover, park's borders are perfectly aligned with North-South and East-West lines. At the same time, royal cricket is always played on a square field that is also aligned with North-South and East-West lines. Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of every tree. This coordinate system is, of course, aligned with North-South and East-West lines. Southwestern corner of the park has coordinates (0, 0) and Northeastern corner of the part has coordinates (W, H), where W and H are the park width and height in feet respectively.
For this task, you may neglect the diameter of the trees. Trees cannot be inside the cricket field, but may be situated on its side. The cricket field may also touch park's border, but shall not lie outside the park.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line of the input file contains three integer numbers N, W, and H, separated by spaces. N ( 0N100) is the number of trees in the park. W and H ( 1W, H10000) are the park width and height in feet respectively.
Next N lines describe coordinates of trees in the park. Each line contains two integer numbers Xi and Yiseparated by a space ( 0XiW, 0YiH) that represent coordinates of i-th tree. All trees are located at different coordinates.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Write to the output file a single line with three integer numbers P, Q, and L separated by spaces, where (P,Q) are coordinates of the cricket field Southwestern corner, and L is a length of its sides. If there are multiple possible field locations with a maximum size, then output any one.
Sample Input
1 7 10 7 3 2 4 2 7 0 7 3 4 5 2 4 1 7
Sample Output
4 3 4
按横坐标排序,然后枚举两个点,推断能构成的最大正方形。然后纵坐标再来一次。
注意初始化的时候能够给四个顶点也放上树。方便处理边界。
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> #include <functional> using namespace std; struct Pt { int x, y; Pt(int a = 0, int b = 0) : x(a), y(b) {} }tree[110]; int n, w, h; int P, Q, L; // 按横坐标排序用 bool cmpx(Pt a, Pt b) { if (a.x != b.x) { return a.x < b.x; } return a.y < b.y; } // 按纵坐标排序用 bool cmpy(Pt a, Pt b) { if (a.y != b.y) { return a.y < b.y; } return a.x < b.x; } void solve() { P = Q = L = 0; // 横向枚举 sort(tree, tree + n, cmpx); for (int i = 0; i < n; i++) { int mny = 0, mxy = h; for (int j = i + 1; j < n; j++) { int l = min(tree[j].x - tree[i].x, mxy - mny); // 能够构成的最大正方形变长 if (l > L) { P = tree[i].x; Q = mny; L = l; } if (tree[j].x == tree[i].x) { // 在同一列失败 continue; } if (tree[j].y > tree[i].y) { mxy = min(mxy, tree[j].y); } else { mny = max(mny, tree[i].y); } } } // 纵向枚举 sort(tree, tree + n, cmpy); for (int i = 0; i < n; i++) { int mnx = 0, mxx = w; for (int j = i + 1; j < n; j++) { int l = min(tree[j].y - tree[i].y, mxx - mnx); if (l > L) { P = mnx; Q = tree[i].y; L = l; } if (tree[j].y == tree[i].y) { continue; } if (tree[j].x > tree[i].x) { mxx = min(mxx, tree[j].x); } else { mnx = max(mnx, tree[j].x); } } } } int main() { ios::sync_with_stdio(false); int T; cin >> T; int kase = 0; while (T--) { cin >> n >> w >> h; for (int i = 0; i < n; i++) { cin >> tree[i].x >> tree[i].y; } // 四个边界也加上树 tree[n++] = Pt(0, 0); tree[n++] = Pt(w, 0); tree[n++] = Pt(0, h); tree[n++] = Pt(w, h); solve(); if (kase++) { cout << endl; } cout << P << ' ' << Q << ' ' << L << endl; } return 0; }