暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

本文详细介绍了如何求解字符串中全0矩形的最大面积,包括暴力解法的实现和优化过程,以及通过逐行遍历计算每对0之间的最大矩形面积。通过实例分析和代码实现,深入探讨了字符串处理和算法优化的关键步骤。

 

题目传送门

 1 /*
 2     题意:求最大矩形(全0)的面积
 3     暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans
 4     注意:是字符串,没用空格,好事多磨,WA了多少次才发现:(
 5     详细解释:http://www.cnblogs.com/cszlg/p/3217478.html
 6 */
 7 #include <cstdio>
 8 #include <algorithm>
 9 #include <cstring>
10 #include <cmath>
11 using namespace std;
12 
13 const int MAXN = 33;
14 const int INF = 0x3f3f3f3f;
15 char s[MAXN][MAXN];
16 int n, m;
17 
18 int get_sum(int x, int y)
19 {
20     int res = 0;    int d, w = 30;
21     for (int i=x; i<n; ++i)
22     {
23         if (s[i][y] == '1')    break;
24         int j = y + 1;
25         while (j < m && s[i][j] == '0')    ++j;
26         d = i - x + 1;
27         if (w > j - y)    w = j - y;
28         res = max (res, (w + d) * 2);
29     }
30 
31     return res;
32 }
33 
34 int main(void)        //Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table
35 {
36     while (scanf ("%d%d", &n, &m) == 2)
37     {
38         for (int i=0; i<n; ++i)
39         {
40             scanf ("%s", s[i]);
41         }
42 
43         int ans = 0;
44         for (int i=0; i<n; ++i)
45         {
46             for (int j=0; j<m; ++j)
47             {
48                 if (s[i][j] == '0')
49                 {
50                     ans = max (ans, get_sum (i, j));
51                 }
52             }
53         }
54 
55         printf ("%d\n", ans);
56     }
57 
58     return 0;
59 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4547670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值