Chapter 1 of algorithm competition-Training Guide-1.22 _ La 3209

Source: Internet
Author: User

This question is also quite good. It is used to find the maximum sub-matrix that meets the conditions, and the method used is very unique.


This question is prone to brute force. Enumeration of each vertex takes each vertex as the start point and the enumeration edge is long. This operation will definitely time out, and the method given in the solution will be very good, compress the surrounding state of each vertex and compress it into a node with the upper and lower sides to express the surrounding state, for example, up [I] [J] indicates the maximum height of the rectangle around the vertex. Left [I] [J] indicates the leftmost left side of the rectangle, right [I] [J] indicates the rightmost distance that the rectangle can reach. After knowing this information, can we scan all the points to see the largest area?

Of course, it is important to update each node. This topic is also very good. When this point cannot be used, the up of this point is 0, and the left breakpoint is 0, the power is n-1 again. Why is this design necessary? Because the updates below are very important (for example, assume that the first row is not usable, but the second row is usable. If your recursive formula is to be used, left [I] [J] = max (left [I-1] [J], lo + 1 ), right [I] [J] = min (right [I] [J], lo-1) assume that the third vertex in the second row can be used at the last vertex, to ensure that all right [I] [J] can be successfully updated, the right endpoint must be n
-1. Please verify it by yourself. Of course, left is the same ).


The difficulty is the establishment of the model and the recurrence and attention of left and right.


Paste the Code:

#include <stdio.h>#include <string.h>#include <string>using namespace std;const int MAXN = 1000 + 11;int N, M;int map[MAXN][MAXN];int up[MAXN][MAXN];int left[MAXN][MAXN];int right[MAXN][MAXN];int main(){char str[10];int T;scanf("%d", &T);while (T--){scanf("%d%d", &N, &M);for (int i = 0; i < N; i++){for (int j = 0; j < M; j++){scanf("%s", str);map[i][j] = (str[0] == 'R') ? 0 : 1;}}int lo = -1;for (int j = 0; j < M; j++){if (map[0][j] == 0){up[0][j] = left[0][j] = 0;lo = j;}else{up[0][j] = 1;left[0][j] = lo + 1;}}lo = M;for (int j = M - 1; j >= 0; j--){if (map[0][j] == 0){right[0][j] = N - 1;lo = j;}else{right[0][j] = lo - 1;}}int ans = 0;for (int i = 1; i < N; i++){lo = -1;for (int j = 0; j < M; j++){if (map[i][j] == 0){up[i][j] = 0;left[i][j] = 0;lo = j;}else{up[i][j] = up[i - 1][j] + 1;left[i][j] = max(left[i - 1][j], lo + 1);}}lo = M;for (int j = M - 1; j >= 0; j--){if (map[i][j] == 0){up[i][j] = 0;right[i][j] = M - 1;lo = j;}else{right[i][j] = min(right[i - 1][j], lo - 1);}int t = (right[i][j] - left[i][j] + 1) * up[i][j];if (ans < t){ans = t;}}}printf("%d\n", ans * 3);}//system("pause");return 0;}/*5 5R R R R RR R R R RR R R F FR R R R RR R R R R*/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.