본문 바로가기
알고리즘

치킨 배달[백준 15686] - python

by 우보틀 2021. 12. 17.

 

 

정답 코드

import sys 
from itertools import combinations

def BOJ15686() :
  N, M = map(int, sys.stdin.readline().split())
  graph = []
  for _ in range(N) :
    graph.append(list(map(int, sys.stdin.readline().split())))

  chiken_store_list = []
  house_list = []

  for x in range(N) :
    for y in range(N) :
      if graph[x][y] == 1 :
        house_list.append([x+1, y+1])
      elif graph[x][y] == 2 :
        chiken_store_list.append([x+1, y+1])

  chiken_store_combination = list(combinations(chiken_store_list, M))
  answer = 1e9
  for chicken_store_lists in chiken_store_combination :
    temp = 0
    for house in house_list :
      temp_length = 1e9
      house_x, house_y = house
      for chicken_store in chicken_store_lists :
        chicken_x, chicken_y = chicken_store
        temp_length = min(temp_length, abs(chicken_x - house_x) + abs(chicken_y - house_y))
      temp += temp_length
    answer = min(answer, temp)
  print(answer)

BOJ15686()

 

접근 방법

1.

 

인사이트

1.

 

 

https://www.acmicpc.net/problem/15686

 

15686번: 치킨 배달

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

www.acmicpc.net

 

'알고리즘' 카테고리의 다른 글

N번째 큰 수[백준 2075번] - python  (0) 2021.12.19
가장 긴 증가하는 부분 수열 3  (0) 2021.12.18
평범한 배낭[백준 12865] - python  (0) 2021.12.16
퍼즐[백준1525] - python  (0) 2021.12.15
리모컨[백준-1107] - python  (0) 2021.12.14