본문 바로가기

OJ

[BOJ] 1051 숫자 정사각형 (JAVA)

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

 

1051번: 숫자 정사각형

N×M크기의 직사각형이 있다. 각 칸에는 한 자리 숫자가 적혀 있다. 이 직사각형에서 꼭짓점에 쓰여 있는 수가 모두 같은 가장 큰 정사각형을 찾는 프로그램을 작성하시오. 이때, 정사각형은 행

www.acmicpc.net

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

public class Main {
	
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int N, M, len, temp;
    static char[][] input;

    public static void main(String[] args) throws Exception {

        st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        input = new char[N][M];
        for (int i = 0; i < N; i++) {
            input[i] = br.readLine().toCharArray();
        }

        for (int i = 0; i < N + 1 - len; i++) {
            for (int j = 0; j < M + 1 - len; j++) {
                temp = check(i, j);
                len = Math.max(len, temp);
            }
        }

        System.out.print(len * len);

    }

    static int check(int i, int j) {
        int add = 1;
        int result = 0;
        char c = input[i][j];

        while (i + add < N && j + add < M) {
            if (c == input[i][j + add] && c == input[i + add][j] && c == input[i + add][j + add]) result = add;
            add++;
        }

        return result + 1;
    }
	
}

'OJ' 카테고리의 다른 글