Search for the largest 1 rectangle in the 01 matrix (poj 3494)

Source: Internet
Author: User

Poj address: http://poj.org/problem? Id = 3494

Summary

The description of the question is very simple. In an M * n matrix, all elements are only 0 and 1, and the largest rectangle containing only 1 is found.

For example, a 4x6 rectangle is shown in the figure, and the red area is the area we want to find.

I first saw this topic in reading the surface Sutra written by a person. At that time, I had no idea how to do it. Later, I learned something called monotonous stack and made some questions. I found that this question on poj is exactly the same as that on the interview question.

So let's take a look.

Question Analysis:

The method for this question is obvious if we use brute force. Find a right bottom endpoint (r, u), and then check whether the square (I, j)-> (R, u) is full 1. if it is all 1, we will find a suitable rectangle 1. find the largest rectangle while enumerating.

In this case, the enumeration complexity is O (M * n), and the complexity of finding the next endpoint is O (M * n). Then the detection complexity is also the end of O (M * n ).AlgorithmThe complexity is O (M ^ 3 * n ^ 3)

Obviously, this complexity is over.

Carefully observe and find out (this idea is also reminded by others, and I feel that I still cannot perform accurate modeling ):

Because we are looking for a rectangle, it must start with a row element. If a rectangle is held, we will find that:

For the first line:

For the second line:

Row 3:

Row 4:

In this case, the biggest rectangle of histogram starting from a row is converted to a rectangle to be found.

Then we can convert the original rectangle into the following data:

The first line indicates the height of histogram formed by using the first line as the bottom edge. Other lines are similar.

So the problem is to enumerate each row, and then find the maximum rectangle of histogram corresponding to each row.

Calculate the maximum rectangle of Histogram

Look at the http://blog.csdn.net/hopeztm/article/details/7868581

After that, the basic principle is the same as that of histogram.

CodeAs follows:

Memory: 31676 ktime: 1782 mslanguage: C ++ result: acceptedsource Code # include <stdio. h> # include <memory. h> # define max_len 2005int nrow, ncol; int matrix [max_len] [max_len]; // the original data int heights [max_len] [max_len]; // use this array to describe histogram. Heights [I] indicates histogram at the bottom of step I. The elements in it indicate the height of the corresponding column, struct node {int height; int position; node (INT _ height, int _ from): height (_ height), position (_ from) {}node () {}}; int max (int A, int B) {Return A> B? A: B;} // implement the stack by yourself. Because the STL stack is too slow, it will time out int topid; node stack [max_len]; // stack corresponding operation void push (const node & T) {stack [topid ++] = T;} const node top () {return stack [topID-1];} void POP () {topid --;} int getarea (INT irow) // use a monotonous stack to enumerate the maximum rectangular area of histogram based on a row. {Topid = 0; push (node (-1, 0); int I; int area, maxarea = 0; int position, height; for (I = 0; I <= ncol; I ++) {position = I + 1; if (I = ncol) {Height =-1 ;} else {Height = heights [irow] [I];} node T (height, position); While (top (). height> height) {T = Top (); POP (); Area = (position-t. position) * t. height; If (area> maxarea) {maxarea = Area ;}} push (node (height, T. position);} return maxarea;} int main () {While (scanf ("% D", & nrow, & ncol )! = EOF) {int I, j; int B; for (I = 0; I <nrow; I ++) {for (j = 0; j <ncol; j ++) {scanf ("% d", & matrix [I] [J]) ;}// obtain histogram. If I acts on the bottom edge, j corresponds to the number of memcpy (heights, matrix, sizeof (matrix); for (I = 0; I <ncol; I ++) from I to the highest consecutive 1) {for (j = 1; j <nrow; j ++) {If (heights [J] [I]! = 0) {heights [J] [I] + = heights [J-1] [I] ;}} int maxarea = 0, area; for (I = 0; I <nrow; I ++) {area = getarea (I); maxarea = max (maxarea, area);} printf ("% d \ n", maxarea );}}

 
 

The stack is self-implemented because the STL stack times out.

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.