HDU 1505 & amp; POJ 1964 City Game (recursive + scanning)

Source: Internet
Author: User

A matrix. Some grids are F and some are R. You need to find the largest sub-matrix so that all the matrices are F.

Idea: enumerate each sub-Matrix directly, obviously the complexity is O (m ^ 3 n ^ 3), obviously TLE.

We can use scanning method: up (I, j) is used to represent a grid (I, j) Suspension Line (the suspension line refers to the line segment before this grid goes up to the grid of the first R). Left (I, j) indicates the position where the suspension line can be moved to the left by scanning the leftmost position (R will be stopped), right (I, j) indicates the position where the drape line can be moved to the rightmost position (R will stop ). Then, the lattice (I, j) can correspond to a maximum F rectangle, with row I as the bottom edge and row I-up (I, j) + 1 as the top bottom edge, the column where left (I, j) is located is the left column, and the column where right (I, j) is located is the right rectangle.

The final answer is the maximum value in the rectangle corresponding to each grid.

Up (I, j) can be recursive: (1) if map [I] [j] = 'F', up (I, j) = up (I-1, j) + 1 (2) if map [I] [j] = 'R', up (I, j) = 0

Left (I, j) can be recursive: (1) if map [I] [j] = 'F', left (I, j) = max {left (I-1, j), lo + 1} (lo is the column 'R' closest to the left of the grid (I, j) (2) if map [I] [j] = 'R', left (I, j) = 0

Right (I, j) can be recursive: (1) if map [I] [j] = 'F', right (I, j) = max {right (I-1, j), ro-1} (ro is the column of the nearest 'r' on the right of the grid (I, j) (2) if map [I] [j] = 'R ', right (I, j) = n

 


Note: The number of data read from this question is large. scanf will use TLE .... Getchar...

 

# Include <cstdio> # include <iostream> # define MAXN 1005 using namespace std; int T, n, m, righ [MAXN] [MAXN], up [MAXN] [MAXN], lef [MAXN] [MAXN]; char map [MAXN] [MAXN]; int main () {scanf ("% d ", & T); while (T --) {int ans = 0; scanf ("% d", & m, & n); getchar (); for (int I = 0; I <m; ++ I) for (int j = 0; j <n; ++ j) {char ch = getchar (); while (ch! = 'F' & ch! = 'R') ch = getchar (); // process the read. Use while until the read is F or R. map [I] [j] = ch ;} for (int j = 0; j <n; ++ j) for (int I = 0; I <m; ++ I) if (map [I] [j] = 'R') up [I] [j] = 0; else up [I] [j] = I? Up [I-1] [j] +; // recursive up (I, j) for (int I = 0; I <m; ++ I) for (int j = 0, lo =-1; j <n; ++ j) if (map [I] [j] = 'R') lo = j, lef [I] [j] = 0; else lef [I] [j] = I? Max (lo + 1, lef [I-1] [j]): lo + 1; // recursive left (I, j) for (int I = 0; I <m; ++ I) for (int j = n-1, ro = n; j> = 0; -- j) if (map [I] [j] = 'R ') ro = j, righ [I] [j] = n; else righ [I] [j] = I? Min (ro-1, righ [I-1] [j]): ro-1; // recursive right (I, j) for (int I = 0; I <m; ++ I) for (int j = 0; j <n; ++ j) if (map [I] [j] = 'F') ans = max (ans, (righ [I] [j]-lef [I] [j] + 1) * up [I] [j]); // calculate the maximum value of the answer printf ("% d \ n", ans * 3);} return 0 ;}

 

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.