곰퓨타의 SW 이야기

[12-07 구현 문제] 치킨 배달 본문

TIL/이것이 코딩테스트다_파이썬 문제 (백준문제 外)

[12-07 구현 문제] 치킨 배달

곰퓨타 2021. 5. 6. 23:21

최근 보고 있는 책인  '이것이 코딩테스트다 with 파이썬 편_나동빈_한빛미디어' 에 있는 문제이다.

www.hanbit.co.kr/store/books/look.php?p_code=B8945183661

 

이것이 취업을 위한 코딩 테스트다 with 파이썬

IT 취준생이라면 누구나 가고 싶어 하는 카카오, 라인, 삼성전자의 2016년부터 2020년까지의 코딩 테스트와 알고리즘 대회의 기출문제를 엄선하여 수록하였다.

www.hanbit.co.kr

 

 

문제는 백준에 있다.

www.acmicpc.net/problem/15686

 

15686번: 치킨 배달

크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸

www.acmicpc.net

 

 

 

import sys
import itertools

input = sys.stdin.readline

n,m = map(int,input().split())
board = []
INF = int(1e9)

for i in range(n):
    board.append(list(map(int,input().split())))

home_arr = []
chicken_arr =[]
for i in range(n):
    for j in range(n):
        if board[i][j] == 1:
            home_arr.append([i,j])
        elif board[i][j]==2:
            chicken_arr.append([i,j])


chick_combi = list(itertools.combinations(chicken_arr,m))

total = INF
for chicken in chick_combi:
    min_total = 0
    for home in home_arr:
        min_temp = INF
        for temp in chicken :
            distance = abs(temp[0]-home[0]) + abs(temp[1]-home[1])
            min_temp = min(min_temp,distance)

        min_total += min_temp

    total = min(total,min_total)


print(total)
Comments