๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1915 ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜• DP

by Jouureee 2021. 2. 5.

๋ฌธ์ œ :

www.acmicpc.net/problem/1915

 

1915๋ฒˆ: ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•

์ฒซ์งธ ์ค„์— n, m(1 ≤ n, m ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ n๊ฐœ์˜ ์ค„์—๋Š” m๊ฐœ์˜ ์ˆซ์ž๋กœ ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

๋ถ„์„ :

๋Œ€ํ‘œ์ ์ธ DP ๋ฌธ์ œ๋กœ 1์”ฉ ๋”ํ•˜๋Š”๊ฒŒ ๊ณง ์ •์‚ฌ๊ฐํ˜•์˜ ํ•œ ๋ณ€์ด ๋œ๋‹ค๋Š” ๊ฒŒ ์‹ ๊ธฐํ–ˆ๋‹ค. DP๊ฐ€ ์ „์— ํ•œ๋ฒˆ ๊ณต๋ถ€ํ–ˆ์„ ๋•Œ ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ๋๋Š”๋ฐ ์ด๋ฒˆ์— ์กฐ๊ธˆ ์•Œ๊ฑฐ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ์šฐ์„  arr์ด๋ผ๋Š” ์›๋ž˜ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  memo๋ผ๋Š” ๋ฐฐ์—ด์— ๊ฐ’์„ ๊ณ„์‚ฐํ•ด ๋ˆ„์ ํ•œ๋‹ค.

arr[i-1][j-1], arr[i-1][j], arr[i][j-1] ๋ชจ๋‘๊ฐ€ 1์ผ๋•Œ๋งŒ์ด arr[i][j]๊ณผ ํ•จ๊ป˜ ์ •์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๋งŒ ๊ฒ€์‚ฌ ํ•œ ๋’ค 1์„ ๋”ํ•ด ํ•œ๋ณ€์„ memo ๋ฐฐ์—ด์— ๋ˆ„์ ํ•ด ๋†“๊ณ  ans์— ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜์—ฌ ์ •์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. 

 

c++ ์ฝ”๋“œ :

//
//  1915_square.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/02/05.
//

#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;

int R1, R2;
int arr[1001][1001] = {0};
int memo[1001][1001] = {0};
string s;
int ans = 0;



void input(){
    cin >> R1 >> R2;
    for(int i = 0; i < R1; i++){
        cin >> s;
        for(int j = 0; j < R2; j++){
            arr[i][j] = s[j] - 48;
            if(arr[i][j] == 1){
                memo[i][j] = 1;
                ans = memo[i][j];
            }
        }
    }
}

void find_biggest_square(){
    for(int i = 1; i < R1; i++){
        for(int j = 1; j < R2; j++){
            if(arr[i-1][j-1] == 1 && arr[i-1][j] == 1 && arr[i][j-1] == 1){
                memo[i][j] = min(memo[i-1][j-1], memo[i-1][j]);
                memo[i][j] = min(memo[i][j], memo[i][j-1]) + 1;
            }
            ans = max(ans, memo[i][j]);
        }
    }
    cout << ans * ans << endl;
    
    
    
}

int main (){
    input();
    find_biggest_square();
    return 0;
}

 

๋Œ“๊ธ€