공간감각이 떨어져서 이런 유형의 구현 문제에 쥐약이다..
진짜 돌겠어서 구글링해서 풀이를 몇 개 찾아서 공부했습니다.
시간 지나고 또 다시 풀기..
/ 코드 1 /
# 출처 : https://hbj0209.tistory.com/m/182
N = int(input())
target = int(input())
# 상, 우, 하, 좌
dx, dy = [-1, 0, 1, 0], [0, 1, 0, -1]
# 처음 1의 위치 x, y에 넣어줌
if N % 2 == 1:
x, y = N//2, N//2
else:
x, y = N//2, N//2 - 1
m = [[0] * N for _ in range(N)]
m[x][y] = 1
cnt = 2
dir = 0
num = 2
while True:
for _ in range(cnt-1):
nx, ny = x + dx[dir], y + dy[dir]
m[nx][ny] = num
num += 1
if num == N**2+1:
break
x, y = nx, ny
if num == N**2+1:
break
dir = (dir + 1) % 4
# 위나 아래로 갈때 숫자 1 증가
if dir == 0 or dir == 2:
cnt += 1
for i in m:
print(*i)
for i in range(N):
for j in range(N):
if m[i][j] == target:
print(i + 1, j + 1)
(solved.ac 티어: 실버 5)
/ 코드 2 /
# 출처: https://my-coding-notes.tistory.com/503
import sys
def draw():
global n
x = y = n//2
cnt = num = 2
d = [(0,1),(1,0),(0,-1),(-1,0)]
t = 0
board[x][y] = 1; x-=1; y-=1
while True:
for _ in range(4):
a,b = d[t]
for _ in range(cnt):
x+=a; y+=b
board[x][y] = num
if num==m:
ans[0]=x+1; ans[1]=y+1
if num==n**2:
return
num+=1
t = (t+1)%4
cnt+=2
x-=1; y-=1
n = int(input())
m = int(input())
board = [[0]*n for _ in range(n)]
ans = [n//2+1,n//2+1]
draw()
for i in board:
print(*i)
print(*ans)
/ 코드 3 /
https://blog.naver.com/PostView.nhn?blogId=repeater1384&logNo=222277064141
[파이썬]백준 1913번: 달팽이
백준 1913번: 달팽이 문제 홀수인 자연수 N(3≤N≤999)이 주어지면, 다음과 같이 1부터 N2까지의 자연수...
blog.naver.com
'CodingTest > Baekjun Online Judge' 카테고리의 다른 글
[ 백준 / BOJ ] 2504 괄호의 값 (0) | 2022.07.01 |
---|---|
[ 백준 / BOJ ] 1966 프린터 큐 (0) | 2022.07.01 |
[ 백준 / BOJ ] 16924 십자가 찾기 (0) | 2022.06.30 |
[ 백준 / BOJ ] 16922 로마 숫자 만들기 (0) | 2022.06.30 |
[ 백준 / BOJ ] 16917 양념 반 후라이드 반 (0) | 2022.06.30 |