Bargaining Table

Bargaining Table
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n × m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.

Input

The first line contains 2 space-separated numbers n and m (1 ≤ n, m ≤ 25) — the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.

Output

Output one number — the maximum possible perimeter of a bargaining table for Bob's office room.

Sample test(s)
input
3 3
000
010
000
output
8
input
5 4
1100
0000
0000
0000
0000
output
16

 这题该怎么说呢,不难,但是又做了挺久。。。

遍历每一个点vi,算出以vi为左上角的谈判桌的最大周长p(vi),取max(p(vi))。有一些简单的剪枝操作,也不值一提。

AC Code:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <queue>
 5 #include <vector>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <cstring>
 9 using namespace std;
10 
11 const int SZ = 26;
12 char map[SZ][SZ];
13 int n, m;
14 
15 int Solve(int y, int x)
16 {
17     int w = 100, d, maxP = 0;
18     for(int i = y; i < n; i++)
19     {
20         int j;
21         for(j = x; j < m; j++)
22         {
23             if(map[i][j] == '1') break;
24         }
25         if(j != x) d = i - y + 1;
26         else break;
27         if(w > j - x) w = j - x;
28         int t = (d + w) << 1;
29         if(t > maxP) maxP = t;
30     }
31     return maxP;
32 }
33 
34 int main()
35 {
36     while(scanf("%d %d", &n, &m) != EOF)
37     {
38         for(int i = 0; i < n; i++)
39             scanf("%s", map[i]);
40         int maxP = 0;
41         for(int i = 0; i < n; i++)
42         {
43             for(int j = 0; j < m; j++)
44             {
45                 if(map[i][j] == '0')
46                 {
47                     int t = Solve(i , j);
48                     if(maxP < t) maxP = t;
49                 }
50             }
51         }
52         printf("%d\n", maxP);
53     }
54     return 0;
55 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值