https://www.acmicpc.net/problem/2447
1, 4, 7, 10, 13, 16...
는 i // 3 == 1 인 수들이다.
이를 찾았다면 다음은 큰 사각형을 처리해야 한다.
입력받은 수의 3의 제곱근을 기준으로 9 부분으로 나눌 수 있는 가장 큰 정사각형 중 가운데 부분을 공백으로 채워준다.
가운데 부분은 3의 제곱근을 기준으로 찾는다.
아래의 start xy, end xy
는 해당 cnt에서 가운데 부분의 시작과 끝 인덱스이다.
# start xy, end xy
sx, ex = x + cnt, x + cnt*2
sy, ey = y + cnt, y + cnt*2
for i in range(sx, ex):
for j in range(sy, ey):
arr[i][j] = ' '
큰 사각형에서 가운데 부분을 비워준 뒤, 작은 사각형으로 다시 실행하여야 하는데, 이 때 가운데 부분만 제외하고 실행해준다.
blank(cnt, x, y)
blank(cnt, sx, y)
blank(cnt, ex, y)
blank(cnt, x, sy)
blank(cnt, ex, sy)
blank(cnt, x, ey)
blank(cnt, sx, ey)
blank(cnt, ex, ey)
# 전부 *로 찍은 다음 공백으로 바꾸자
def blank(cnt, x, y):
if cnt == 1: return
cnt = cnt // 3
# start xy, end xy
sx, ex = x + cnt, x + cnt*2
sy, ey = y + cnt, y + cnt*2
for i in range(sx, ex):
for j in range(sy, ey):
arr[i][j] = ' '
blank(cnt, x, y)
blank(cnt, sx, y)
blank(cnt, ex, y)
blank(cnt, x, sy)
blank(cnt, ex, sy)
blank(cnt, x, ey)
blank(cnt, sx, ey)
blank(cnt, ex, ey)
n = int(input())
arr = [["*"]*n for _ in range(n)]
blank(n, 0, 0)
for el in arr:
print(''.join(el))