*문제 출처는 백준에 있습니다.
문제 제목: 제곱수 찾기 / 1025번 (골드 5단계)
문제 사이트: https://www.acmicpc.net/problem/1025
문제 설명

나의 풀이
import sys
input = sys.stdin.readline
def is_perfect_square(s):
num = int(s)
root = int(num ** 0.5)
return root * root == num
def main():
N, M = map(int, input().split())
board = [list(input().strip()) for _ in range(N)]
max_square = -1
for i in range(N): # 시작 행
for j in range(M): # 시작 열
for dx in range(-N, N):
for dy in range(-M, M):
if dx == 0 and dy == 0:
continue
x, y = i, j
s = ""
while 0 <= x < N and 0 <= y < M:
s += board[x][y]
if is_perfect_square(s):
max_square = max(max_square, int(s))
x += dx
y += dy
print(max_square)
if __name__ == "__main__":
main()

※ 알아야 할 것
for dx in range(-N + 1, N):
for dy in range(-M + 1, M):
혹시나 범위를 이렇게 -N + 1, -M + 1로 잡으시면 손실이 발생할 수 있습니다!
'Coding Test > 백준-Python' 카테고리의 다른 글
| 백준 / 수 묶기 / 1744번 / Python (0) | 2025.04.25 |
|---|---|
| 백준 / 영역 구하기 / 2583번 / Python (0) | 2025.04.23 |
| 백준 / N과 M (10) / 15664번 / Python (0) | 2025.04.17 |
| 백준 / 숨바꼭질 3 / 13549번 / Python (0) | 2025.04.14 |
| 백준 / 최대 곱 / 1500번 / Python (0) | 2025.04.14 |