일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Flutter
- JAVA SPRING
- 스프링
- 페이코 초대코드
- C언어
- 플러터 개발환경 설정
- 최단 경로
- php 프로그래밍 입문
- php 프로그래밍 입문 연습문제
- 한정 분기
- 백준
- spring
- programmers
- SWEA
- 페이코 친구코드
- php 프로그래밍 입문 솔루션
- C
- php 프로그래밍
- 배열
- php 프로그래밍 입문 예제
- 플러터
- 페이코 추천인코드
- php 프로그래밍 입문 3판
- 자바 스프링
- 페이코 추천인
- 자바
- php 프로그래밍 입문 문제풀이
- Java
- 파이썬
- php
Archives
- Today
- Total
ImJay
[SW Expert Academy] 12712. 파리퇴치3 본문
반응형
[SW Expert Academy] 12712. 파리퇴치3
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
# 테스트 케이스의 수를 입력받습니다.
T = int(input())
# 각 테스트 케이스에 대해 반복합니다.
for test_case in range(1, T+1):
# 행과 열의 크기를 입력받습니다.
N, M = map(int, input().split())
# 2차원 리스트를 입력받습니다.
lst = [list(map(int, input().split())) for _ in range(N)]
# 최종 결과를 저장할 변수를 초기화합니다.
ans = 0
# 각 요소에 대해 반복하여 최대 합을 찾습니다.
for i in range(N):
for j in range(N):
# 가로 방향 합 계산
cnt_plus = lst[i][j]
for k in range(j-M+1, j+M):
if 0 <= k < N and k != j:
cnt_plus += lst[i][k]
# 세로 방향 합 계산
for l in range(i-M+1, i+M):
if 0 <= l < N and l != i:
cnt_plus += lst[l][j]
# 대각선 방향 합 계산 (x자 모양)
cnt_x = lst[i][j]
for m in range(-M+1, M):
dx, dy = i+m, j+m
if 0 <= dx < N and 0 <= dy < N and (dx != i and dy != j):
cnt_x += lst[dx][dy]
dx, dy = i+m, j-m
if 0 <= dx < N and 0 <= dy < N and (dx != i and dy != j):
cnt_x += lst[dx][dy]
# 최대 합 갱신
ans = max(ans, cnt_plus, cnt_x)
# 결과 출력
print(f'#{test_case} {ans}')
반응형
Comments