-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|