Codevs 2491 Jade Toad Hall

Source: Internet
Author: User

Codevs 2491 Jade Toad Hallhttp://codevs.cn/problem/2491/Title Description Description

One day, the kitten Rainbow and Freda came to the west Hunan Zhangjiajie Tianmen Mountain Jade Toad Hall, Jade Toad Hall Palace main Blue Rabbit hospitable to them, and give them a piece of land.

This piece of land is divided into n*m grids, each of which reads ' R ' or ' F ', and R represents the land that was given to rainbow,f to represent the land was given Freda.
Now Freda is here to sell Moe ... It is looking for a rectangular piece of land, requiring the land to be labeled ' F ' and the largest area.
But Rainbow and Freda Oi level are weak burst, can't find this piece of land, and blue Rabbit also want to see Freda sell Moe (she obviously won't program ...) So they decide that if you find a land area of s, they each give you s two silver.

Enter a description Input Description

The first row of two integers n,m, indicating that the rectangular land has n rows M columns.
The next n lines, each with a space-separated character ' F ' or ' R ', describe the rectangular land.

Output description Output Description

Output An integer that indicates how much money you can get, i.e. the value of the maximum ' F ' rectangle land area.

Sample input Sample Input

5 6
R F F f f f
F F f f f f
R-R R F F F
F F f f f f
F F f f f f

Sample output Sample Output

45

Data range and Tips Data Size & Hint

For 50% of data, 1<=n,m<=200
For 100% of data, 1<=n,m<=1000

Method One: The explanation of maximal sub-matrix is not very good, please refer to: 2003 National Training Team paper, Wang Zhiqun, talking about solving maximal sub-rectangular problem with maximal thought http://wenku.baidu.com/view/6b348c0203d8ce2f006623bb.html

This problem can be translated into finding a maximum sub-matrix of all f in the matrix.

The largest sub-matrix of all f can be seen by a rectangle with a width of 1 and a high H.

So we can draw the algorithm:

If a[i][j]=f, set to A[i][j] as the datum point, the maximum height can be extended to H, that is, the height of the matrix. What is the maximum height? The height at which the upper boundary or R stops when it reaches the top.

Set left[i][j] means a[i][j] moves to pan left, up to which column to pan. What is the maximum translation to which column? Stop when looking left, reaching the left border or having r in this column.

Set right[i][j] means a[i][j] to the right, to which column to pan. Principle Ibid.

Then the maximum number of sub-matrices (right[i][j]-left[i][j]+1) can be obtained by extending the point A[i][j].

How do you determine these three numbers? Do you need to search for every a[i][j]? No.

We can pass the first dimension I through the loop, leaving only the second dimension J. Then you can implement h[],right[],left[] in N-line recursive trial, O (1) complexity determination.

Here's how:

First, initialize the condition: All left[]=1,right[]= columns, indicating unlimited extension, h[]=0, height of 0

Set current loop to line I

Set L indicates that the first r,r to the right touches the first R that comes to the left when J points are reached from the bank.

l=0,r= number of columns at initial +1

a. H[j]=p indicates the maximum height that can be extended from line I to a[i][j] as the datum point.

1, if A[I][J]=R,H[J] is set to 0, the line I has been used over h[j], and the line I and the following, because I act R, block the following matrix and the above connected to a matrix, so to set 0.

2, if a[i][j]=f,h[j]++, that can be connected with the previous line into a matrix, the height of +1 from the previous row.

B. Left[j] Represents a datum point with a[i][j], which extends up to the left, that is, the maximum number of columns.

1, if a[i][j]=r,

①:A[I][J] is set to 1, because left has only one dimension and is listed as subscript, so the next time to use Left[j] is the next line, that is, a[i+1][j], then because of the block I, the first row of i+1 can not be connected with the front of a matrix, so set the initial condition.

②: Update l=j.

2, if A[i][j]=f,left[j]=max (left[j],l+1). Because Left[i] is the result of the I-1 line before the update, it should be larger from row i-1 and line I. L+1 is because L has the position of R, and the rectangle cannot extend to the place where R is.

C. Right[j] Represents a datum point with a[i][j], which extends up to the right, that is, the minimum number of columns.

1, then if A[i][j]=r,

①:A[I][J] is set to M, because right has only one dimension and is listed as subscript, so the next time you use Right[j] is the next line, that is, a[i+1][j], then because of the block I, the first row of i+1 can not be connected with the front of a matrix, so set the initial condition.

②: Update r=j.

2, if A[i][j]=f,right[j]=min (right[j],r-1). Because Right[i] is the result of the I-1 line before the update, it is taken from line i-1 and line I. R-1 is because R is the location of R, and the rectangle cannot extend to a place with R.

D. Update ans

#include <cstdio>
#include<iostream>using namespacestd;intN,m,ans;Charx;inta[1001][1001],h[1001],leftt[1001],rightt[1001];intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {cin>>x; if(x=='R') a[i][j]=1;//1 stands for obstruction, no extension. } for(intj=1; j<=m;j++)//Initialize{Leftt[j]=1; RIGHTT[J]=m; } for(intI=1; i<=n;i++)//Enumerate each row { intL=0, r=m+1;//l The first obstacle to the left, R to the right first to encounter the obstacle for(intj=1; j<=m;j++)//enumerate each column from left to right to determine L { if(A[i][j])//The current point is an obstacle{L=j;//update the first obstacle to the lefth[j]=0;//re-counting from the bank's sub-matrix heightleftt[j]=1;//because left has only one dimension and is listed as subscript, so the next time to use Left[j] is the next line, that is, a[i+1][j], then because of the block I, the first line of i+1 can not be connected with the front of a matrix, so set as the original condition } Else//the current point is not an obstacle, you can participate in constituent matrices{H[j]++;//Current column height +1Leftt[j]=max (leftt[j],l+1);//Left[i] is the result of the I-1 line before the update, so take it from row i-1 and line I. L+1 is because L has the position of r, and the rectangular extension cannot reach the place where there is a barrier. } } for(intj=m;j>=1; j--)//Update R to determine the first r found to the right { if(A[i][j]) {R=J; RIGHTT[J]=L; } ElseRightt[j]=min (rightt[j],r-1); Ans=max (ans, (rightt[j]-leftt[j]+1)*H[j]); }} printf ("%d", ans*3);}

Codevs 2491 Jade Toad Hall

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.