[SW Expert Academy, SWEA 1979] 어디에 단어가 들어갈 수 있을까 (python)
Problem Solving/SWEA

[SW Expert Academy, SWEA 1979] 어디에 단어가 들어갈 수 있을까 (python)

728x90

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PuPq6AaQDFAUq&categoryId=AV5PuPq6AaQDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=1

 

SW Expert Academy

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

swexpertacademy.com


※ SW Expert 아카데미의 문제를 무단 복제하는 것을 금지합니다.

728x90

내 생각

처음에 문제를 잘못 읽어서 0으로 채워진 칸과 1로 채워진 칸이 그냥 칸 구분용이고, 0에도 단어를 넣을 수 있는 줄 알았다.

한 번 틀리고 나서, 문제를 제대로 읽어보니 단어는 1에만 넣고 0에는 단어를 넣을 수 없었다.

즉, 1이 K번 연속으로 나오는 구간만 찾으면 된다.

문제를 잘 읽자!

T = int(input())

for test_case in range(1, T + 1):
    N, K = map(int, input().split())

    puzzle = [list(map(int, input().split())) for _ in range(N)]

    #가능한 단어 갯수 카운트
    count = 0
    
    #가로부터 확인
    for i in range(N):
        #1이 반복해서 나온 횟수
        word = 0
        for j in range(0, N):
            #해당 칸이 1이면 단어 길이 +1
            if puzzle[i][j] == 1:
                word += 1
            #0이면
            else:
                #전 칸까지 연속된 1의 길이가 K였다면 count +1
                if word == K:
                    count += 1
                #word를 다시 0으로 세팅
                word = 0
        #줄바꿈이 있을 때, 마지막 칸이 1 연속 K칸이었다면 count +1
        if word == K:
            count += 1

    #세로 확인(가로와 같은 방법으로)
    for i in range(N):
        word = 0
        for j in range(0, N):
            if puzzle[j][i] == 1:
                word += 1
            else:
                if word == K:
                    count += 1
                word = 0
        if word == K:
            count += 1

    print("#{} {}".format(test_case, count))

가로, 세로 한 번에 확인

T = int(input())

for test_case in range(1, T + 1):
    N, K = map(int, input().split())

    puzzle = [list(map(int, input().split())) for _ in range(N)]

    #가능한 단어 갯수 카운트
    count = 0
    
    for i in range(N):
        #1이 반복해서 나온 횟수
        word_row = 0
        word_column = 0
        for j in range(0, N):
            #해당 칸이 1이면 단어 길이 +1
            #가로
            if puzzle[i][j] == 1:
                word_row += 1
            else:
                #전 칸까지 연속된 1의 길이가 K였다면 count +1
                if word_row == K:
                    count += 1
                #word를 다시 0으로 세팅
                word_row = 0
            #세로
            if puzzle[j][i] == 1:
                word_column += 1
            else:
                if word_column == K:
                    count += 1
                word_column = 0
        #줄바꿈이 있을 때, 마지막 칸이 1 연속 K칸이었다면 count +1
        if word_row == K:
            count += 1
        if word_column == K:
            count += 1

    print("#{} {}".format(test_case, count))
728x90