程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforce 22B Bargaining Table

Codeforce 22B Bargaining Table

編輯:C++入門知識

Codeforce 22B Bargaining Table


B. Bargaining Table

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

HDU 1505,1506的變形 只是由求面積變成了求周長 具體分析可見http://blog.csdn.net/iooden/article/details/38379065

#include
#include
#include
using namespace std;
const int N = 30;
int l[N][N], r[N][N], h[N][N], n, m, ans;
char a[N][N];
int main()
{
    scanf ("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
    {
        h[i][0] = h[i][m + 1] = -1;
        scanf ("%s", a[i] + 1);
        for (int j = 1; j <= m; ++j)
        {
            if (a[i][j] == '0') h[i][j] = h[i - 1][j] + 1;
            l[i][j] = r[i][j] = j;
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = m; j >= 1; --j)
            while (h[i][r[i][j] + 1] >= h[i][j]&&h[i][j])
                r[i][j] = r[i][r[i][j] + 1];

        for (int j = 1; j <= m; ++j)
        {
            while (h[i][l[i][j] - 1] >= h[i][j]&&h[i][j])
                l[i][j] = l[i][l[i][j] - 1];
            ans = max (ans, r[i][j] - l[i][j] + 1 + h[i][j]);
        }
    }
    printf ("%d\n", 2 * ans);
    return 0;
}

另外這題數據比較小 也可以暴力枚舉 枚舉每點作為左上角 然後枚舉合法的的長和寬, 判斷形成的的矩陣是否全由 '0'組成, 如果是就更新結果

#include
#include
const int maxn = 30;
int n, m;
int a[maxn][maxn];
int sum[maxn][maxn];
int main()
{
    while (~scanf ("%d%d", &n, &m))
    {
        memset (a, 0, sizeof (a));
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                scanf ("%1d", &a[i][j]);
        memset (sum, 0, sizeof (sum));
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + a[i][j];
            }
        }
        int x, y;
        int ans = 0;
        for (int x1 = 1; x1 <= n; x1++)
            for (int y1 = 1; y1 <= m; y1++)
                for (int x2 = x1; x2 <= n; x2++)
                    for (int y2 = y1; y2 <= m; y2++)
                    {
                        int tmp = sum[x2][y2] + sum[x1 - 1][y1 - 1] - sum[x1 - 1][y2] - sum[x2][y1 - 1];
                        if (tmp == 0)
                        {
                            int dx = x2 - x1 + 1;
                            int dy = y2 - y1 + 1;
                            if (ans < (dx + dy) * 2) ans = (dx + dy) * 2;
                        }
                    }
        printf ("%d\n", ans);
    }
    return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved