일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- DP
- 탐욕법
- heap
- 프로그래머스
- 코딩테스트
- BFS
- string
- 백준
- 정렬
- Queue
- 카카오 기출
- DFS
- DVWA
- 그래프
- sort
- binary search
- Code Refactoring
- Algorithm
- 힙
- Greedy
- Dynamic Programming
- programmers
- 문자열
- 동적계획법
- 큐
- 알고리즘
- graph
- django
- 완전탐색
- Brute Force
Archives
- Today
- Total
생각과 고민이 담긴 코드
백준 - 치킨 배달 (완전 탐색) / Gold 5 / 15686번 본문
문제 : https://www.acmicpc.net/problem/15686
15686번: 치킨 배달
크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸
www.acmicpc.net
풀이
import sys
from itertools import combinations
def calculate(stores):
result = 0
for k in home:
result += min([abs(l[0] - k[0]) + abs(l[1] - k[1]) for l in stores])
return result
n, m = map(int, sys.stdin.readline().split())
answer = []
table = []
store = []
home = []
for _ in range(n):
table.append(list(map(int, sys.stdin.readline().split())))
for i in range(n):
for j in range(n):
if table[i][j] == 2:
store.append([i, j])
elif table[i][j] == 1:
home.append([i, j])
close = len(store) - m
targets = list(combinations(store, close))
for target in targets:
temp = store[:] # 재할당할땐 복사본으로. (그렇지 않으면 temp과 store가 같은 메모리 주소를 공유하기 때문에 둘다 바뀜)
for i in target:
temp.remove(i)
answer.append(calculate(temp))
print(min(answer))
이 문제도 N의 크기를 확인하여 완전 탐색이 가능한지 판단해야 한다.
N의 최대 크기가 50이고 치킨집의 수도 최대 13이기 때문에 폐업할 집을 선택하는 조합의 수을 감안하더라도
완전 탐색으로 풀어도 되겠다는 판단을 했다.
1이 존재하는 [행 번호, 열 번호] , 2가 존재하는 [행 번호, 열 번호] 형태로 관리하여
쉽게 최소거리를 판단할 수 있었다.
'Algorithm > 백준' 카테고리의 다른 글
백준 - 단지번호붙이기 (DFS/BFS) / Silver 1 / 2667번 (0) | 2021.11.22 |
---|---|
백준 - 연구소 (DFS, BFS, 완전탐색) / Gold 5 / 14502번 (0) | 2021.11.17 |
백준 - 트리의 부모 찾기 (DFS, BFS) / Silver 2 / 11725번 (0) | 2021.11.17 |
백준 - 물건 팔기 (정렬, 완전 탐색) / Silver 3 / 1487번 (0) | 2021.11.12 |
백준 - 연산자 끼워넣기 (완전 탐색) / Silver 1 / 14888번 (0) | 2021.11.02 |