BOJ1025: 제곱수 찾기 작성일 2020-04-05 https://www.acmicpc.net/problem/1025 일단 이해하기가 거지같은 문제였다. 제곱수를 체크할때는 sqrt를 쓰는것도 나쁘지 않다. 0도 제곱수이다. 123456789101112131415161718192021222324252627282930313233343536373839#include <bits/stdc++.h> using namespace std; int N, M, ans = -1; vector<vector<int>> arr; unordered_set<int> sq; bool bound(int x, int y) { return x >= 0 && y >= 0 && x < N && y < M; } void check(int x, int y, int dx, int dy) { int ret = arr[x][y]; if(sq.count(ret)) ans = max(ans, ret); if (!dx && !dy) return; while(1) { x += dx, y += dy; if(!bound(x, y)) return; ret = ret * 10 + arr[x][y]; if(sq.count(ret)) ans = max(ans, ret); } } int main() { scanf("%d %d", &N, &M); arr.resize(N, vector<int>(M)); for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) scanf("%1d", &arr[i][j]); for(int i = 0; i * i <= 999999999; i++) sq.insert(i*i); for(int x = 0; x < N; x++) for(int y = 0; y < M; y++) for(int dx = -N; dx <= N; dx++) for(int dy = -M; dy <= M; dy++) check(x, y, dx, dy); cout << ans; }