BOJ1025: 제곱수 찾기

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

  • 일단 이해하기가 거지같은 문제였다.
  • 제곱수를 체크할때는 sqrt를 쓰는것도 나쁘지 않다.
  • 0도 제곱수이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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;
}