SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
그림에 그린 순서대로
상하좌우 다이렉트 배열을 만들고,
벽과 만나지 않으면 k를 1씩 추가하면서 M만큼 더한다.
def kill1(y, x):
global total1
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
k = 0
# idx = 0
total1 += arr[y][x]
while k<M-1:
idx = 0
k += 1
while idx<4:
ny = y+dy[idx]*k
nx = x+dx[idx]*k
if nx<0 or ny<0 or nx>N-1 or ny>N-1:
idx +=1
continue
if idx>3:
idx = 0
break
total1 += arr[ny][nx]
idx += 1
위에 상하좌우 더했던거랑 똑같은데
다이렉트배열만 대각선으로 바꾸면 된다.
def kill2(y, x):
global total2
dy = [-1, 1, -1, 1]
dx = [-1, -1, 1, 1]
k = 0
# idx = 0
total2 += arr[y][x]
while k<M-1:
idx = 0
k += 1
while idx<4:
ny = y+dy[idx]*k
nx = x+dx[idx]*k
if nx<0 or ny<0 or nx>N-1 or ny>N-1:
idx +=1
continue
if idx>3:
idx = 0
break
total2 += arr[ny][nx]
idx += 1
NxN 배열을 돌면서 모든 경우에 대해서 위 두 함수를 실행하고,
그 값들이 최댓값보다 크면 대체한다.
T = int(input())
for tc in range(1, 1+T):
N, M = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(N)]
MAX = -21e8
for i in range(N):
for j in range(N):
total1 = 0
total2 = 0
kill1(i, j)
if total1>MAX:
MAX = total1
kill2(i, j)
if total2>MAX:
MAX = total2
print(f'#{tc} {MAX}')
'파이썬 알고리즘' 카테고리의 다른 글
[파이썬] 백준 10944 별 찍기 -19 (0) | 2022.10.19 |
---|---|
[파이썬] SWEA 12712 파리퇴치3 (for 문으로 풀기) (0) | 2022.10.18 |
[파이썬] SWEA 2001 파리 퇴치 (0) | 2022.10.18 |
[파이썬] SWEA 5789 현주의 상자 바꾸기 (0) | 2022.10.12 |
[파이썬] SWEA 6485 삼성시의 버스노선 (0) | 2022.10.12 |