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
", maxP);
53     }
54     return 0;
55 }
原文地址:https://www.cnblogs.com/cszlg/p/3217478.html