Maximum P1387 square, P1387 Square

Source: Internet
Author: User

Maximum P1387 square, P1387 Square
Description

Find the maximum square that does not contain 0 in a matrix containing only 0 and 1 of n * m, and the length of the output side.

Input/Output Format Input Format:

The first row of the input file is two integers, n, m (1 <= n, m <= 100), followed by n rows. Each row contains m numbers, which are separated by spaces, 0 or 1.

Output Format:

An integer with the side length of the largest square

Input and Output sample Input example #1:
4 40 1 1 11 1 1 00 1 1 01 1 0 1
Output sample #1:
2
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 int map [101] [101]; 7 int dp [101] [101]; 8 int main () 9 {10 int n, m; 11 scanf ("% d", & n, & m ); 12 for (int I = 1; I <= n; I ++) 13 for (int j = 1; j <= m; j ++) 14 {15 scanf ("% d", & map [I] [j]); 16 if (map [I] [j]) 17 dp [I] [j] = 1; 18} 19 for (int I = 1; I <= n; I ++) 20 for (int j = 1; j <= m; j ++) 21 {22 if (map [I] [j]) 23 {24 dp [I] [j] = min (dp [I-1] [j], dp [I] [J-1]), dp [I-1] [J-1]) + 1; 25} 26} 27 int ans =-1; 28 for (int I = 1; I <= n; I ++) 29 {30 for (int j = 1; j <= m; j ++) 31 {32 ans = max (ans, dp [I] [j]); 33} 34} 35 printf ("% d", ans); 36 return 0; 37}AC 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 int map [101] [101]; 7 int dp [80] [80] [80] [80]; 8 int main () 9 {10 int n, m; 11 scanf ("% d ", & n, & m); 12 for (int I = 1; I <= n; I ++) 13 for (int j = 1; j <= m; j ++) 14 {15 scanf ("% d", & map [I] [j]); 16 if (map [I] [j]) 17 dp [I] [j] [I] [j] = 1; 18} 19 20 for (int I = 1; I <= n; I ++) 21 {22 for (int j = 1; j <= m; j ++) 23 {24 if (map [I] [j]) 25 {26 for (int k = I; k <= n; k ++) 27 {28 for (int l = j; l <= m; l ++) 29 {30/* if (k-1 = 0) 31 dp [I] [j] [k] [l] = dp [I] [j] [k] [l] | (dp [I] [j] [k] [L-1] & map [k] [l]); 32 else if (L-1 = 0) 33 dp [I] [j] [k] [l] = dp [I] [j] [k] [l] |] [l] & map [k] [l]); 34 else */35 dp [I] [j] [k] [l] = dp [I] [j] [k] [l] | (dp [I] [j] [k-1] [l] | dp [I] [j] [k] [L-1]) & map [k] [l]); 36} 37} 38} 39 40} 41} 42 for (int I = 1; I <= n; I ++) 43 {44 for (int j = 1; j <= m; j ++) 45 {46 if (map [I] [j]) 47 {48 for (int k = I; k <= n; k ++) 49 {50 for (int l = j; l <= m; l ++) 51 {52/* if (k-1 = 0) 53 dp [I] [j] [k] [l] = dp [I] [j] [k] [l] | (dp [I] [j] [k] [L-1] & map [k] [l]); 54 else if (L-1 = 0) 55 dp [I] [j] [k] [l] = dp [I] [j] [k] [l] |] [l] & map [k] [l]); 56 else */57 if (dp [I] [j] [k-1] [l-1] & dp [I] [j] [k-1] [l] & dp [I] [j] [k] L-1] & map [k] [l] = 1) 58 dp [I] [j] [k] [l] = max (dp [I] [j] [k] [l], dp [I] [j] [k-1] [L-1] + 1); 59} 60} 61} 62 63} 64} 65 int ans =-1; 66 for (int I = 1; I <= n; I ++) 67 {68 for (int j = 1; j <= m; j ++) 69 {70 for (int k = 1; k <= n; k ++) 71 {72 for (int l = 1; l <= m; l ++) 73 {74 ans = max (ans, dp [I] [j] [k] [l]); 75} 76} 77} 78} 79 printf ("% d ", ans); 80 return 0; 81}Four-Dimensional 30

 

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.