Hihocoder #1042
Time Limit: 10000 ms single-point time limit: 1000 ms memory limit: 256 MB
Description
As soon as I woke up, Little Hi crossed back to ancient times! Thanks to the good deeds of the enemy, the great sweat rewards Little Hi to drive his horse on the enemy's grassland. After riding a horse in the grassland within one day, the farm of Little Hi will be displayed. But what makes little Hi a headache is that there is a stinking pond on the enemy's grassland. Little Hi cannot ride into the stinking pond, and even if the riding path of Little Hi is surrounded by a stinking pond, Little Hi's ox horse cannot graze in the stinking pond.
In order to more scientifically circle the earth, Xiao Hi simplified and abstracted the problem: (1) the enemy's grassland is a square matrix of n × m. (2) the horse riding path is a closed line along the edge of the square. (3) The stinking pond is a rectangle in the matrix, and (4) the circumference of the horse riding path does not exceed L. Xiao Hi wants to know how much grassland he can enclose (the area of the smelly pond is not included ).
: Figure 1 is a valid path; Figure 2 is also a valid path, but the area of the enclosed grassland is 0; Figure 3 is not a valid path because it is not closed; figure 4 is not a valid path because it passes through the pond.
Input
3 integers in the first row: n, m, L (1 <= n, m <= 100, 1 <= L <= 400)
Four integers in the second row: l, r, t, B (0 <= l <r <= m, 0 <= t <B <= n) coordinates of the Left, right, top, and bottom boundary of the pond.
Output
The maximum area that small Hi can enclose
-
Sample Input
-
4 4 81 3 1 3
-
Sample output
-
3
Obviously, the larger the area covered by L is, the larger the area. Therefore, this question should be an enumeration of all the areas when the circumference is L.
We mainly consider three situations:
1. No overlap between the enclosure and pond
2. A vertex of the pond is inside the enclosure
3. The four vertices of the pond are in the enclosure
You can change the ponds to the upper-right corner of the grassland Through coordinate transformation.
The coordinates of the pond center are used to determine whether the pond is located in the upper left, upper right, lower left, or lower right.
Transformation of pond coordinates
Int l1, r1, t1, b1; if (l + r <= m & t + B <= n) // {l1 = m-r in the lower left corner; r1 = m-l; t1 = n-B; b1 = n-t;} else if (l + r> = m & t + B <= n) // {l1 = l; r1 = r; t1 = n-B; b1 = n-t ;} else if (l + r <= m & t + B> = n) // {l1 = m-r; r1 = m-l; t1 = t; b1 = B ;}
It can be simplified:
int l1, r1, t1, b1;l1 = l;r1 = r;t1 = t;b1 = b;if (l + r <= m) {l1 = m - r;r1 = m - l;}if (t + b <= n){t1 = n - b;b1 = n - t;}
The complete procedure is as follows:
#include
using namespace std;int main(){int n, m, L, l, r, t, b;cin >> n >> m >> L;cin >> l >> r >> b >> t;if (L >= 2 * (m + n)){cout << m*n - (r - l)*(t - b) << endl;return 0;}int l1, r1, t1, b1;l1 = l;r1 = r;t1 = t;b1 = b;if (l + r <= m) {l1 = m - r;r1 = m - l;}if (t + b <= n){t1 = n - b;b1 = n - t;}int ans = 0;for (int i = 1; i < L / 2 && i <= m; i++){for (int j = 1; j <= L / 2 - i&&j <= n; j++){if (i <= l1 || j <= b1)ans = ans >i*j ? ans : i*j;else if (i > l1 && i <= r1 && j > b1 && j <= t1)ans = ans > i*j - (i - l1)*(j - b1) ? ans : i*j - (i - l1)*(j - b1);else if (i >= r1&&j >= t1)ans = ans > i*j - (r - l)*(t - b) ? ans : i*j - (r - l)*(t - b);elsecontinue;}}cout << ans << endl;system(pause);return 0;}
( ̄ 3 ̄) づ