Zoshin's book-the largest 01 sub matrix in the Programmer's Code interview guide-C + + implementation

Source: Internet
Author: User
Tags assert int size
Topic:
Given an integer matrix map, the values are only 0 and 12, and the largest rectangular area is 1 of all rectangular regions that are all 1.
1 0 1 1
1 1 1 1
1 1 1 0

Of these, the largest rectangular area has 6 1, so it returns 6.


Train of thought: in each row to do cutting, statistics in the current row as the bottom of the case, each position up to the number of consecutive 1, using the height of the array of heights to represent.
When the first line is cut, the height = {1,0,1,1}.
When the second line is cut, the height = {2,1,2,2}.
After the third line is cut, Heigth = {3,2,3,0}.




We use a stack to store the subscript for the height array, and when the stack is empty or the current element's value is greater than the stack top element (Height[s.top ()), it is pushed directly into the stack.
When the stack is not empty and the current element's value is less than or equal to the top of the stack, the settlement is the right edge of the current element's subscript, and the value below the top of the stack is the left edge. The height is the current element, and the size of the matrix is obtained.
The top element of the stack is ejected, repeating the action until the current element is larger than the top of the stack or the stack is empty, pressing into the current element.


#include <iostream> using namespace std; #include <cassert> #include <stack> int maxrecfrombotton (int *height, int size) {assert (height &&

	Size > 0);
	int max = 0;

	Stack<int> s; for (int i = 0; i < size; ++i)//traverse this array {while (!s.empty () && height[i] <= height[s.top ())//equals stack top element
		Also to settle.
			{Int J = s.top ();
			S.pop (); int k = S.empty ()? -1:s.top ();
			When the stack is empty, the left bound is-1, otherwise the value below the top of the stack, the right side equals the subscript of the current element. int cur = height[j] * (i-k-1);
			The width of the matrix is (i-k-1). Because the left and right bounds are inaccessible, such as the left boundary is-1, the right side is 3, and the width is "0,1,2" equals 3. max = cur > Max?
		Cur:max; } s.push (i);
		When the current element is larger than the top of the stack, Direct push}//When the array is finished, do not forget that there may be elements in the stack, while (!s.empty ()) {int j = s.top ();
		S.pop (); int k = S.empty ()?  -1:s.top ();
		Left border. int cur = height[j] * (size-k-1);
		When the array is finished traversing the right boundary is the array size. max = cur > Max? 
	Cur:max;
return to Max;
	int maxrecsize (int map[][4],int row) {assert (map && row > 0);
	int col = 4;
	int max = 0; int *height = new Int[col];

	memset (height,0,sizeof (height) *col);   for (int i = 0; i < row; ++i) {for (int j = 0; J < col; ++j) {height[j] = map[i][j] = = 0? 0:height[j]+1;
		The elements of the array are expressed as the number of consecutive 1 at the bottom of the current behavior.
		int cur = Maxrecfrombotton (height,col); max = cur > Max?
	Cur:max;
return to Max;
	int main () {int map[3][4] = {{1,0,1,1}, {1,1,1,1}, {1,1,1,0}};
	int res = maxrecsize (map, 3);

	cout << Res <<endl;
	cout << "Hello ..." <<endl;
	System ("pause");
return 0; }


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.