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

Sarah·2021년 8월 29일
0

SWE

목록 보기
11/19

문제출처 : SW Expert Academy

문제

주어진 퍼즐 모양에서 특정 길이 K를 갖는 단어가 들어갈 수 있는 자리의 수를 출력하는 프로그램을 작성하라.

코드

# 0829
import sys
sys.stdin = open('input.txt')

def ans(arr):
    global N, K
    result = 0
    ans = []
    cnt = 0
    # 행검사
    for i in range(N):
        result = 0
        for j in range(N):
            if arr[i][j] == 1:
                result += 1
                if j == N-1:
                    ans.append(result)
            else:
                if result != 0:
                    ans.append(result)
                result = 0

    # 열검사
    for i in range(N):
        result = 0
        for j in range(N):
            if arr[j][i] == 1:
                result += 1
                if j == N-1:
                    ans.append(result)
            else:
                if result != 0:
                    ans.append(result)
                result = 0
                
    # 문자길이와 같은 공간 갯수 찾기
    for w in ans:
        if w == K:
            cnt += 1

    return cnt

T = int(input())
for tc in range(1, T+1):
    # N x N 행렬 / K 넣을 글자 길이
    N, K = list(map(int, input().split()))
    arr = [list(map(int, input().split())) for _ in range(N)]

    print("#{} {}".format(tc, ans(arr)))

장애물

  1. 글자길이 3일때 4개의 칸이 있는 경우를 안되는 경우로 어떻게 구별할까 고민하는 게 힘들었다.
    -> else 아래 if 문이 해결해준다.
profile
2021.06 ~

0개의 댓글