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' 카테고리의 다른 글
[BOJ] 4883 삼각 그래프 (JAVA) (0) | 2023.04.07 |
---|---|
[BOJ] 11723 집합 (JAVA) (0) | 2023.04.06 |
[BOJ] 2126 지진 (JAVA) (0) | 2023.04.04 |
[BOJ] 14574 헤븐스 키친 (JAVA) (0) | 2023.04.03 |
[BOJ] 15595 정답 비율 계산하기 (JAVA) (0) | 2023.04.02 |