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

HDU4328 Cut the cake(动规:最大子矩形问题/悬线法)

时间:2018-10-23 20:49:02      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:条件   har   iostream   class   分享   cstring   cst   ima   php   

题目链接:传送门

题目大意:

  给出N*M的字符矩阵(由字符B/R组成),求符合下图条件的子矩阵的最大周长。

  1 ≤ N,M ≤ 1000。

                        技术分享图片

思路:

  悬线法。

技术分享图片
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
const int MAX_N = 1e3 + 5;

int N, M;
char mat[MAX_N][MAX_N];
int lef[MAX_N][MAX_N], rig[MAX_N][MAX_N], up[MAX_N][MAX_N];

void init1()
{
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++)
            if (j > 1 && mat[i][j] == mat[i][j-1])
                lef[i][j] = lef[i][j-1] + 1;
            else
                lef[i][j] = 1;
        for (int j = M; j >= 1; j--)
            if (j < M && mat[i][j] == mat[i][j+1])
                rig[i][j] = rig[i][j+1] + 1;
            else
                rig[i][j] = 1;
    }
}

void init2()
{
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++)
            if (j > 1 && mat[i][j] != mat[i][j-1])
                lef[i][j] = lef[i][j-1] + 1;
            else
                lef[i][j] = 1;
        for (int j = M; j >= 1; j--)
            if (j < M && mat[i][j] != mat[i][j+1])
                rig[i][j] = rig[i][j+1] + 1;
            else
                rig[i][j] = 1;
    }
}

int dp1()
{
    int ans = 0;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (i > 1 && mat[i][j] == mat[i-1][j]) {
                up[i][j] = up[i-1][j] + 1;
                lef[i][j] = min(lef[i][j], lef[i-1][j]);
                rig[i][j] = min(rig[i][j], rig[i-1][j]);
            }
            else
                up[i][j] = 1;
            int len = lef[i][j] + rig[i][j] - 1;
            int high = up[i][j];
            ans = max(ans, 2*len+2*high);
        }
    }
    return ans;
}

int dp2()
{
    int ans = 0;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (i > 1 && mat[i][j] != mat[i-1][j]) {
                up[i][j] = up[i-1][j] + 1;
                lef[i][j] = min(lef[i][j], lef[i-1][j]);
                rig[i][j] = min(rig[i][j], rig[i-1][j]);
            }
            else
                up[i][j] = 1;
            int len = lef[i][j] + rig[i][j] - 1;
            int high = up[i][j];
            ans = max(ans, 2*len + 2*high);
        }
    }
    return ans;
}

int main()
{
    int T;
    int kase = 1;
    cin >> T;
    while (T--) {
        cin >> N >> M;
        for (int i = 1; i <= N; i++)
            for (int j = 1; j <= M; j++)
                cin >> mat[i][j];
        int ans = 1;
        init1();
        ans = max(ans, dp1());
        init2();
        ans = max(ans, dp2());
        printf("Case #%d: %d\n", kase++, ans);
    }
    return 0;
}
/*
2
3 3
BBR
RBB
BBB
1 1
B
*/
View Code

 

HDU4328 Cut the cake(动规:最大子矩形问题/悬线法)

标签:条件   har   iostream   class   分享   cstring   cst   ima   php   

原文地址:https://www.cnblogs.com/Lubixiaosi-Zhaocao/p/9838212.html

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