Skip to content

Commit

Permalink
7/2 PS 치킨 배달
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoiEungi committed Jul 2, 2021
1 parent 7f88295 commit a1c89ca
Showing 2 changed files with 54 additions and 38 deletions.
33 changes: 33 additions & 0 deletions choieungi/12_13 치킨 배달.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
from itertools import combinations

Input = sys.stdin.readline

n, m = map(int, Input().split())
space = [Input().split() for _ in range(n)]
chicken = []
home = []


def min_distance(case, home):
ans = 0
for p,q in home:
distance = int(1e9)
for x,y in case:
distance = min(distance, abs(p-x)+abs(q-y))
ans += distance
return ans


for i in range(n):
for j in range(n):
if space[i][j] == '1': home.append((i,j))
elif space[i][j] == '2': chicken.append((i,j))

answer = int(1e9)
for case in combinations(chicken, m):
answer = min(answer, min_distance(case, home))

print(answer)


59 changes: 21 additions & 38 deletions choieungi/main.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,33 @@
import sys
from collections import deque
from itertools import combinations

Input = sys.stdin.readline

n = int(Input())
apple_number = int(Input())
n, m = map(int, Input().split())
space = [Input().split() for _ in range(n)]
chicken = []
home = []

space = [[0]*n for _ in range(n)]
direction = [(-1, 0), (0, 1), (1, 0), (0, -1)] # up, right, down, left

for _ in range(apple_number):
x, y = map(int, Input().split())
space[x-1][y-1] = 1
def min_distance(case, home):
ans = 0
for p,q in home:
distance = int(1e9)
for x,y in case:
distance = min(distance, abs(p-x)+abs(q-y))
ans += distance
return ans

move_number = int(Input())

move_list = [tuple(map(lambda x: 1 if x == "D" else (3 if x == "L" else int(x)), Input().split())) for _ in range(move_number)]
move_list = deque(move_list)
for i in range(n):
for j in range(n):
if space[i][j] == '1': home.append((i,j))
elif space[i][j] == '2': chicken.append((i,j))

# MARK: - Game Start
second = 0
snake = [[0, 0]]
my_direction = 1

while True:
nx = snake[-1][0] + direction[my_direction][0]
ny = snake[-1][1] + direction[my_direction][1]
second += 1

if nx >= n or nx < 0 or ny >= n or ny < 0:
print(second)
break
if [nx, ny] in snake and [nx, ny] != snake[-1]:
print(second)
break

if space[nx][ny] == 1:
snake.append([nx, ny])
space[nx][ny] = 0
else:
snake.pop(0)
snake.append([nx, ny])

if move_list:
if second == move_list[0][0]:
my_direction = ((my_direction + move_list.popleft()[1]) % 4)
answer = int(1e9)
for case in combinations(chicken, m):
answer = min(answer, min_distance(case, home))

print(answer)


0 comments on commit a1c89ca

Please sign in to comment.