-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
319df19
commit 50c4110
Showing
7 changed files
with
173 additions
and
1 deletion.
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,52 @@ | ||
import sys | ||
import heapq | ||
input = sys.stdin.readline | ||
|
||
N = int(input()) | ||
graph = [list(map(int, input().split())) for _ in range(N)] | ||
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] | ||
heap = [] | ||
|
||
|
||
def init(): | ||
for i in range(N): | ||
for j in range(N): | ||
if graph[i][j] == 9: | ||
heapq.heappush(heap, (0, i, j)) | ||
graph[i][j] = 0 | ||
return | ||
|
||
|
||
def bfs(): | ||
baby_shark_size, baby_shark_cnt, answer = 2, 0, 0 | ||
visited = [[False]*N for _ in range(N)] | ||
while heap: | ||
cnt, q_x, q_y = heapq.heappop(heap) | ||
if 0 < graph[q_x][q_y] < baby_shark_size: | ||
baby_shark_cnt += 1 | ||
graph[q_x][q_y] = 0 | ||
if baby_shark_size == baby_shark_cnt: | ||
baby_shark_size += 1 | ||
baby_shark_cnt = 0 | ||
answer += cnt | ||
cnt = 0 | ||
while heap: | ||
heap.pop() | ||
visited = [[False]*N for _ in range(N)] | ||
for i in range(4): | ||
s_x, s_y = q_x+dx[i], q_y+dy[i] | ||
if 0 <= s_x < N and 0 <= s_y < N: | ||
if graph[s_x][s_y] > baby_shark_size or visited[s_x][s_y]: | ||
continue | ||
heapq.heappush(heap, (cnt+1, s_x, s_y)) | ||
visited[s_x][s_y] = True | ||
print(answer) | ||
|
||
|
||
init() | ||
bfs() | ||
|
||
|
||
|
||
|
||
|
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,56 @@ | ||
import sys | ||
from collections import deque | ||
import heapq | ||
input = sys.stdin.readline | ||
N = int(input()) | ||
graph=[] | ||
for i in range(N): | ||
row = list(map(int, input().split())) | ||
for j in range(N): | ||
if row[j]==9: | ||
start = (i,j,0) | ||
graph.append(row) | ||
print(graph) | ||
dx=[-1,1,0,0] | ||
dy=[0,0,-1,1] | ||
|
||
|
||
def bfs(start,graph,shark_size): | ||
queue = deque() | ||
queue.append(start) | ||
x,y,t=start | ||
graph[x][y] = 0 | ||
visited = set() | ||
result = [] | ||
while queue: | ||
x,y,t = queue.popleft() | ||
visited.add((x,y)) | ||
for i in range(4): | ||
nx=x+dx[i] | ||
ny=y+dy[i] | ||
if 0<=nx<N and 0<=ny<N and (nx,ny) not in visited: | ||
visited.add((nx,ny)) | ||
if graph[nx][ny]==0 or graph[nx][ny]==shark_size: | ||
queue.append((nx,ny,t+1)) | ||
continue | ||
if graph[nx][ny] > shark_size: | ||
continue | ||
else: | ||
heapq.heappush(result,(t+1,nx,ny)) | ||
if result: | ||
return result[0] | ||
return False | ||
time = 0 | ||
shark_size = 2 | ||
eated = 0 | ||
while True: | ||
eating_fish = bfs(start,graph,shark_size) | ||
if not eating_fish:break | ||
t,nx,ny = eating_fish | ||
time += t | ||
eated += 1 | ||
if eated == shark_size: | ||
shark_size+=1 | ||
eated=0 | ||
start = (nx,ny,0) | ||
print(time) |
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,23 @@ | ||
import sys | ||
from itertools import combinations | ||
input = sys.stdin.readline | ||
|
||
N, M = map(int, input().split()) | ||
graph = [list(map(int, input().split())) for _ in range(N)] | ||
location_home, location_chicken, answer2 = [], [], [] | ||
|
||
for index1, row in enumerate(graph): | ||
for index2, val in enumerate(row): | ||
if val == 1: location_home.append((index1, index2)) | ||
elif val == 2: location_chicken.append((index1, index2)) | ||
|
||
for chicken in combinations(location_chicken, M): | ||
sum = 0 | ||
for h_x, h_y in location_home: | ||
answer = [] | ||
for c_x, c_y in chicken: | ||
answer.append(abs(c_x-h_x) + abs(c_y-h_y)) | ||
sum += min(answer) | ||
answer2.append(sum) | ||
|
||
print(min(answer2)) |
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,22 @@ | ||
import sys | ||
from itertools import combinations | ||
input = sys.stdin.readline | ||
|
||
N, M = map(int, input().split()) | ||
graph = [list(map(int, input().split())) for _ in range(N)] | ||
location_home, location_chicken = [], [] | ||
|
||
for index1, row in enumerate(graph): | ||
for index2, val in enumerate(row): | ||
if val == 1: location_home.append((index1, index2)) | ||
elif val == 2: location_chicken.append((index1, index2)) | ||
|
||
minv = float('inf') | ||
for chicken in combinations(location_chicken, M): | ||
sum = 0 | ||
for home in location_home: | ||
sum += min([abs(chi[0]-home[0]) + abs(chi[1]-home[1]) for chi in chicken]) | ||
if minv > sum: | ||
minv = sum | ||
|
||
print(minv) |
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
Empty file.
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,20 @@ | ||
import sys | ||
from itertools import combinations | ||
input = sys.stdin.readline | ||
|
||
answer = [] | ||
in_value = [int(input()) for _ in range(9)] | ||
|
||
# combinations를 사용하여 in_value에 존재하는 값 중에서 7개의 값을 뽑은 경우를 구한다 | ||
# permutations과 다르게 combinations는 순서는 상관하지 않는다.(조합) | ||
all_cases = combinations(in_value, 7) | ||
|
||
# 합이 100인 경우가 답이다. | ||
for case in all_cases: | ||
if sum(case) == 100: | ||
answer = case | ||
break | ||
|
||
# 값을 한 개씩 출력하기 위해 for문 사용 | ||
for i in answer: | ||
print(i) |