접근 : 조건
- 연결된 섬들의 값 합 계산 → bfs
- 어디에 섬이 존재하는 지 확인 → 이중 for문
문제 : 이모티콘 할인행사
https://school.programmers.co.kr/learn/courses/30/lessons/154540
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
from collections import deque
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def solution(maps):
col, row = len(maps), len(maps[0])
ground = []
vst = [[0]*row for _ in range(col)] # 주의
def bfs(c,r):
q = deque([[c,r]])
result = 0
while q:
a,b = q.popleft()
result += int(maps[a][b])
for i in range(4):
if 0 <= a+dy[i] < col and 0 <= b+dx[i] < row and maps[a+dy[i]][b+dx[i]] != 'X' and vst[a+dy[i]][b+dx[i]] == 0:
q.append([a+dy[i], b+dx[i]])
vst[a+dy[i]][b+dx[i]] = 1
ground.append(result)
for c in range(col):
for r in range(row):
if maps[c][r] != 'X' and vst[c][r] == 0:
vst[c][r] = 1
bfs(c,r)
if len(ground) == 0:
return [-1]
ground.sort()
return ground
'알고리즘' 카테고리의 다른 글
[python -> java] 코딩테스트 언어 연습 1 (1) | 2024.01.02 |
---|---|
[java] queue 사용 (0) | 2024.01.02 |
[프로그래머스] 이모티콘 할인행사 (0) | 2023.11.29 |
[프로그래머스] 투포인터 (1) | 2023.11.27 |
[프로그래머스] 백트래킹 (0) | 2023.11.22 |