码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷 P1387 最大正方形 题解

时间:2017-09-19 11:56:51      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:max   ons   getc   简便   stream   输入   lin   const   www.   

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。

题目链接:https://www.luogu.org/problem/show?pid=1387

题目描述

在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长。

输入输出格式

输入格式:

输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m个数字,用空格隔开,0或1.

输出格式:

一个整数,最大正方形的边长

输入输出样例

输入样例#1:
4 4
0 1 1 1
1 1 1 0
0 1 1 0
1 1 0 1
输出样例#1:
2

分析:
看到有国家集训队的神犇研究过这个问题,做法是悬线法。
不过这题可以直接DP,而且做法非常简便qwq
出自某dalao的博客。取min保证组出正方形,画一画图大概就能想通。


AC代码:
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 
 5 const int MAXN = 105;
 6 
 7 inline void read(int &x)
 8 {
 9     char ch = getchar(),c = ch;x = 0;
10     while(ch < 0 || ch > 9) c = ch,ch = getchar();
11     while(ch <= 9 && ch >= 0) x = (x<<1)+(x<<3)+ch-0,ch = getchar();
12     if(c == -) x = -x;
13 }
14 
15 inline int min(int a,int b)
16 {return a<b?a:b;}
17 
18 int n,m,ans,mp[MAXN][MAXN],f[MAXN][MAXN];
19 
20 int main()
21 {
22     read(n),read(m);
23     for(int i = 1;i <= n;++ i)
24         for(int j = 1;j <= m;++ j)
25         {
26             read(mp[i][j]);
27             if(mp[i][j]) f[i][j] = min(f[i-1][j-1],min(f[i-1][j],f[i][j-1]))+1;
28             if(f[i][j] > ans) ans = f[i][j];
29         }
30     printf("%d\n",ans);
31     return 0;
32 }

 

洛谷 P1387 最大正方形 题解

标签:max   ons   getc   简便   stream   输入   lin   const   www.   

原文地址:http://www.cnblogs.com/shingen/p/7549968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!