20210115 // 삼성 SW Expert 아카데미 // 1979

2021. 1. 15. 13:18Programming/SW Expert Academy

삼성 SW Expert 아카데미 1979번 "어디에 단어가 들어갈 수 있을까" 문제입니다.

TC = int(input())
 
def rotate_90(m):
  N = len(m)
  ret = [[0] * N for _ in range(N)]
 
  for r in range(N):
      for c in range(N):
          ret[c][N-1-r] = m[r][c]
  return ret
 
for tc in range(1, TC+1):
  N, K = map(int, input().split())
  lst = [list(map(int, input().split())) for _ in range(N)]
  rotlst = rotate_90(lst)
  plc = 0
  for i in range(N):
      for j in range(N-K+1):
          if j == 0:
              if sum(lst[i][j:j+K]) == K and lst[i][j+K]==0:
                  plc += 1
          elif j == N-K:
              if sum(lst[i][j:j+K]) == K and lst[i][j-1]==0:
                  plc += 1
          else:
              if  sum(lst[i][j:j+K]) == K and lst[i][j+K]==0 and lst[i][j-1]==0:
                  plc += 1
  for i in range(N):
      for j in range(N-K+1):
          if j == 0:
              if sum(rotlst[i][j:j+K]) == K and rotlst[i][j+K]==0:
                  plc += 1
          elif j == N-K:
              if sum(rotlst[i][j:j+K]) == K and rotlst[i][j-1]==0:
                  plc += 1
          else:
              if  sum(rotlst[i][j:j+K]) == K and rotlst[i][j+K]==0 and rotlst[i][j-1]==0:
                  plc += 1
  print("#%s"%tc, plc)

0 1이 배열된 2차원 배열 좌표를 받고 이를 rotate_90 함수를 이용하여 90도 돌린다. 그 후에 해당 길이만큼 자리가 있는지 파악한다.

 

방법은 리스트의 인덱스에서 해당 단어의 길이만큼 sum 함수를 사용하여 그 빈칸의 길이(1의 수)가 단어의 길이와 같으면 단어가 들어갈 수 있는 공간이 있는 것이다.

 

공간이 들어갈 수 있으면 카운트 +1을 한다. 돌리기 전과 돌린 후 둘 다 적용하여 카운트를 세면 된다.