알고리즘/백준
[백준] 1913번번 (python 파이썬)
G_Gi
2021. 3. 24. 17:19
SMALL
구현 문제이다.
시작 좌표를 구한다.
x = n / 2
y = n / 2
시작좌표에서 시작하여 상, 우, 하, 좌를 돌면서 1씩 더해간다.
이동 횟수는 정수(제곱근수) 만큼 이동한다.
ex)
상
2
2의 제곱근 1.xxxx
위로 한칸까지 이동할 수 있다.
1칸 이동했으므로 오른쪽 확인
우
3
3의 제곱근 1.xxxx
오른쪽으로 한칸까지 이동할 수 있다.
1칸 이동했으므로 아래 확인
하
4
4의 제곱근 2
아래로 두칸까지 이동할 수 있다.
하
5
5의 제곱근 2
2칸 이동했으므로 왼쪽확인
.......
n = int(input())
target = int(input())
graph = list([0] * n for _ in range(n))
s_x, s_y = n // 2, n // 2
graph[s_x][s_x] = 1
move = 1
up, u_check = 0, False
right, r_check = 0, False
down, d_check = 0, False
left, l_check = 0, False
for i in range(2, n * n + 1):
move = int((i - 1) ** (1 / 2))
if not u_check:
if up < move:
s_x -= 1
graph[s_x][s_y] = i
up += 1
continue
else:
u_check = True
up = 0
if not r_check:
if right < move:
s_y += 1
graph[s_x][s_y] = i
right += 1
continue
else:
r_check = True
right = 0
if not d_check:
if down < move:
s_x += 1
graph[s_x][s_y] = i
down += 1
continue
else:
d_check = True
down = 0
if not l_check:
if left < move:
s_y -= 1
graph[s_x][s_y] = i
left += 1
if left != move:
continue
else:
l_check = True
left = 0
u_check, r_check, d_check, l_check = False, False, False, False
answer_x, answer_y = -1, -1
for i in range(n):
for j in range(n):
print(graph[i][j], end=' ')
if graph[i][j] == target:
answer_x, answer_y = i, j
print()
print(answer_x+1, answer_y+1)
LIST