녕의 학습 기록

백준_1915 가장 큰 정사각형 (DP) 본문

Algorithm/Algorithm 문제

백준_1915 가장 큰 정사각형 (DP)

kjyyjk 2024. 4. 21. 00:37

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BJ_1915_가장큰정사각형 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        int[][] a = new int[n+1][m+1];
        int[][] d = new int[n+1][m+1];

        int i, j;
        String s;
        for (i=1; i<n+1; i++) {
            s = br.readLine();
            for (j=1; j<m+1; j++) {
                a[i][j] = Integer.parseInt(String.valueOf(s.charAt(j-1)));
            }
        }

        int max = 0;
        for (i=1; i<n+1; i++) {
            for (j=1; j<m+1; j++) {
                if (a[i][j] == 1) {
                    d[i][j] = Math.min(Math.min(d[i-1][j], d[i][j-1]), d[i-1][j-1]) + 1;

                    if (d[i][j] > max) {
                        max = d[i][j];
                    }
                }
            }
        }

        int result = max * max;
        System.out.println(new StringBuilder().append(result));

    }

}
 

1915번: 가장 큰 정사각형

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

www.acmicpc.net

 

'Algorithm > Algorithm 문제' 카테고리의 다른 글

백준_2342 Dance Dance Revolution (DP)  (0) 2024.04.23
백준_1328 고층 빌딩 (DP)  (0) 2024.04.21
백준_9252 LCS 2 (DP)  (1) 2024.04.21
백준_13398 연속합 2 (DP)  (0) 2024.04.18
백준_10844 쉬운 계단 수 (DP)  (0) 2024.04.16