From a1c89cab1b182dabfc4536d00a4da898aec6fdc4 Mon Sep 17 00:00:00 2001 From: ChoiEungi Date: Fri, 2 Jul 2021 16:00:46 +0900 Subject: [PATCH] =?UTF-8?q?7/2=20PS=20=EC=B9=98=ED=82=A8=20=EB=B0=B0?= =?UTF-8?q?=EB=8B=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\355\202\250 \353\260\260\353\213\254.py" | 33 +++++++++++ choieungi/main.py | 59 +++++++------------ 2 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 "choieungi/12_13 \354\271\230\355\202\250 \353\260\260\353\213\254.py" diff --git "a/choieungi/12_13 \354\271\230\355\202\250 \353\260\260\353\213\254.py" "b/choieungi/12_13 \354\271\230\355\202\250 \353\260\260\353\213\254.py" new file mode 100644 index 0000000..e7e3e5a --- /dev/null +++ "b/choieungi/12_13 \354\271\230\355\202\250 \353\260\260\353\213\254.py" @@ -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) + + diff --git a/choieungi/main.py b/choieungi/main.py index 9962b36..e7e3e5a 100644 --- a/choieungi/main.py +++ b/choieungi/main.py @@ -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)