ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 가장 큰 정사각형 - 1915
    Algorithm/BOJ 2020. 3. 1. 19:20
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    //1915 - 가장 큰 정사각형
     
    #include <iostream>
    using namespace std;
     
    int N, M;
    int arr[1000][1000];
    int cache[1000][1000];
    int ans;
     
    void init()
    {
        cin >> N >> M;
        char c;
        for (int i = 0; i < N; ++i)
            for (int j = 0; j < M; ++j)
            {
                cin >> c;
                arr[i][j] = c - '0';
                cache[i][j] = -1;
            }
        ans = cache[0][0= arr[0][0];
    }
     
    int dp(int x, int y)
    {
        if (x < 0 || x == N || y < 0 || y == M)
            return 0;
        int &ret = cache[x][y];
        if (ret != -1)
            return ret;
        if (!arr[x][y])
            ret = 0;
        else if (!dp(x - 1, y) || !dp(x, y - 1))
            ret = 1;
        else if (dp(x - 1, y) == dp(x, y - 1))
            if (arr[x - dp(x - 1, y)][y - dp(x - 1, y)])
                ret = dp(x - 1, y) + 1;
            else
                ret = dp(x - 1, y);
        else
            ret = min(dp(x - 1, y), dp(x, y - 1)) + 1;
        ans = max(ans, ret);
        return ret;
    }
     
    int main()
    {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        init();
        for (int i = 0; i < N; ++i)
            for (int j = 0; j < M; ++j)
                dp(i, j);
        cout << ans * ans;
    }
    cs

     

     

     

     

     

     

    https://www.acmicpc.net/problem/1915

     

    1915번: 가장 큰 정사각형

    첫째 줄에 n, m(1 ≤ n, m ≤ 1,000)이 주어진다. 다음 n개의 줄에는 m개의 숫자로 배열이 주어진다.

    www.acmicpc.net

     

     

     

     

     

     

    가장 큰 정사각형

    C(x, y) : (x, y)를 우하단 꼭지점으로 하는 정사각형중 가장 큰것의 한 변의 길이

    C(x, y)는 3가지 경우를 고려해 구할수 있습니다.

    1. (x, y)가 0인경우

    2. C(x-1, y) , C(x, y-1) 중 하나라도 0인 경우

    3. C(x-1, y) == C(x, y-1) 인 경우

    4. 그 외의 경우

     

    위 경우를 모두 고려해 코딩해주면 됩니다!

     

     

     

     

     

     

    'Algorithm > BOJ' 카테고리의 다른 글

    파일 합치기 - 11066  (0) 2020.03.02
    가장 긴 바이토닉 부분 수열 - 11054  (0) 2020.03.01
    2225 - 합분해  (0) 2020.03.01
    동전 2 - 2294  (0) 2020.03.01
    동전 1 - 2293  (0) 2020.03.01

    댓글

Designed by Tistory.