백준 문제풀이/Implementation

C++] 백준 문제풀이 (Implementation) 1051 숫자 정사각형

코딩준우 2023. 6. 27. 00:17

 

 

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

 

1051번: 숫자 정사각형

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

www.acmicpc.net

 

 

 

//[C++] 백준 문제풀이 (Implementation)

#include <bits/stdc++.h>

std::string map[50];
int n, m;

int ret = 1;

int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> n >> m;
    for (int i = 0; i < n; ++i){
        std::cin >> map[i];
    }

    for (int y = 0; y < n; ++y){
        for (int x = 0; x < m; ++x){
            for (int k = 1;; ++k){
                if (y + k >= n || x + k >= m) break;
                if (map[y][x] == map[y + k][x] &&
                    map[y][x] == map[y][x + k] &&
                    map[y][x] == map[y + k][x + k]){
                    ret = std::max(ret, (k + 1) * (k + 1));
                }
            }
        }
    }
    
    std::cout << ret << "\n";
    return 0;
}