Hdu4328 cut the cake (motion gauge: Maximum subrectangle problem/suspension method)

Source: Internet
Author: User

Question link: Portal

Question:

The N * m character matrix (consisting of character B/R) is given, and the maximum perimeter of the Child matrix that meets the conditions is obtained.

1 ≤ n, m ≤ 1000.

Ideas:

Suspension method.

#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;}/*23 3BBRRBBBBB1 1B*/
View code

 

Hdu4328 cut the cake (motion gauge: Maximum subrectangle problem/suspension method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.