Question 1497: full matrix with the largest area

Source: Internet
Author: User
/* Description: In an M * n matrix, all the elements are only 0 and 1. From this matrix, find a fully-1 sub-matrix with the largest area, the maximum is the maximum number of elements 1. Input: the input may contain multiple test examples. For each test case, the first line of input is two integers m, n (1 <= m, n <= 1000): represents the size of the matrix to be input. There are m rows in the Matrix. Each row has n integers, which are 0 or 1 respectively. The adjacent two numbers are strictly separated by a space. Output: For each test case, the number of elements in the matrix with the largest area in all 1 child matrices is displayed. Sample input: 2 20 00 04 40 0 0 00 1 1 00 1 00 0 0 0 0 sample output: 04 train of thought: each vertex, scan it up, left, the boundary derived from the right is up [I] [J] * (right [I] [J]-left [I] [J] + 1) */# include <stdio. h> const int n= 1001; int map [N] [N], up [N] [N], left [N] [N], right [N] [N]; int max (int x, int y) {If (x> Y) return X; return y;} int min (int x, int y) {If (x <Y) return X; return y;} int main () {int n, m; while (scanf ("% d", & M, & N )! = EOF) {for (INT I = 0; I <m; I ++) for (Int J = 0; j <n; j ++) scanf ("% d", & map [I] [J]); int ans = 0; For (INT I = 0; I <m; I ++) {int Lo =-1; int RO = N; For (Int J = 0; j <n; j ++) {If (Map [I] [J] = 0) {up [I] [J] = 0; left [I] [J] = 0; Lo = J ;} else {if (I = 0) {up [I] [J] = 1; left [I] [J] = lo + 1 ;} else {up [I] [J] = up [I-1] [J] + 1; left [I] [J] = max (Lo + 1, left [I-1] [J]) ;}}for (Int J = n-1; j> = 0; j --) {If (Map [I] [J] = 0) {right [I] [J] = N; RO = J;} else {if (I = 0) {right [I] [J] = ro-1;} else {right [I] [J] = min (ro-1, right [I-1] [J]);} ans = max (ANS, up [I] [J] * (right [I] [J]-left [I] [J] + 1 ));}} printf ("% d \ n", ANS);} 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.