SWEA

1979. 어디에 단어가 들어갈 수 있을까

zhelddustmq 2024. 7. 15. 10:19

문제: 무단 배포 금지로 인해 사이트 주소로 남깁니다.

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PuPq6AaQDFAUq

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

정답 코드:


T = int(input())
# 여러개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
for test_case in range(1, T + 1):
    answer = 0
    N, K = map(int, input().split())
    arr = [list(map(int, input().split())) for _ in range(N)]
    #밑과 오른쪽만 조사(중복 방지)
    for c in range(N):
        for r in range(N):
            #빈칸을 발견하면
            if arr[c][r]:
                #빈칸이 시작하는 곳일 때만 검사하기(좌 우)
                if not arr[c][r-1] or r == 0 :
                    i = 0
                    count = 0
                    temp = 1
                    while True:
                        if r + i >= N:
                            break
                        temp = arr[c][r+i]
                        if temp == 0:
                            break
                        i += 1
                        count += 1
                    if count == K:
                        answer += 1
                #빈칸이 시작하는 곳일 때만 검사하기(상 하)
                if not arr[c-1][r] or c == 0:
                    i = 0
                    count = 0
                    temp = 1
                    while True:
                        if c + i >= N:
                            break
                        temp = arr[c + i][r]
                        if temp == 0:
                            break
                        i += 1
                        count += 1
                    if count == K:
                        answer += 1
    print(f"#{test_case}", answer)

'SWEA' 카테고리의 다른 글

1284. 수도 요금 경쟁  (0) 2024.07.15
1945. 간단한 소인수분해  (0) 2024.07.15
1954 .달팽이 숫자  (0) 2024.07.15
1959. 두 개의 숫자열  (0) 2024.07.15
1961. 숫자 배열 회전  (0) 2024.07.15