알고리즘
백준 1051번 숫자 정사각형 C++
영춘권의달인
2021. 10. 21. 12:21
간단한 브루트포스 문제이다. 모든 칸을 돌면서 오른쪽, 아래쪽으로만 검사하면서 정사각형이 되는지 확인하고 크기가 커질때마다 크기를 갱신해주었다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
string str;
vector<vector<int>> v(n, vector<int>(m));
for (int i = 0; i < n; i++) {
cin >> str;
for (int j = 0; j < m; j++) {
v[i][j] = str[j]-'0';
}
}
int max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int size = 1;
int currVal = v[i][j];
while (1) {
if (i + size >= n || j + size >= m) break;
if (v[i][j + size] == currVal && v[i + size][j] == currVal && v[i + size][j + size] == currVal) {
if (size > max) {
max = size;
}
}
size++;
}
}
}
cout << (max + 1) * (max + 1);
};