프로그래밍/알고리즘

[Python] 백준 1388번 - 바닥장식

고등어찌짐 2022. 7. 7. 06:38

 

n, m = map(int, input().split())
arr = []
for i in range(n):
  arr.append(input())

cnt = 0
for i in range(n):
  rows = arr[i].split('|')
  for r in rows:
    if r != '':
      cnt += 1

for j in range(m):
  temp = []
  for k in range(n):
    temp.append(arr[k][j])

  temp_str = ''.join(temp)
  if '-' not in temp_str :
    cnt += 1
  else :
    cols = temp_str.split('-')
    for c in cols:
      if c != '':
        cnt += 1

print(cnt)


원래는 깊이우선탐색 문제를 풀려고 했던건데, 문자열로 푸는 방법을 찾아냈다. 먼저 하나의 row 를 문자열 전체로 받는다. row 에서 | 를 기준으로 split 하면 연속된 - 가 붙어있는채로 split 할 수 있다. column 에서는 row 처럼 이어진 문자열을 생성해준 다음 - 를 기준으로 split 한다.