Skip to content

Commit b53d39f

Browse files
committed
kakao 2021
1 parent 17c7142 commit b53d39f

File tree

2 files changed

+112
-31
lines changed

2 files changed

+112
-31
lines changed

python/kakao/2021_6.py

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,67 @@
11
import sys
22
input = sys.stdin.readline
3-
def solution(sales, links):
4-
adj = [[] for _ in range(len(sales))]
5-
for a,b in links:
6-
adj[a-1].append(b-1)
7-
dp_arr = [[-1,-1] for _ in range(len(sales))]
8-
def dp(now,check):
9-
if len(adj[now])==0:
3+
import heapq
4+
d = [(0,-1),(0,1),(-1,0),(1,0)]
5+
INF = 1e9
6+
dp_arr = [[[-1]*(1<<7) for _ in range(4)] for _ in range(4)]
7+
8+
def solution(board, r, c):
9+
board_dict = {}
10+
11+
def dfs(y,x,y1,x1,n):
12+
st = [(0,y,x)]
13+
visited = [[False]*4 for _ in range(4)]
14+
visited[y][x]=True
15+
while st:
16+
cnt,y,x = heapq.heappop(st)
17+
if y==y1 and x==x1:
18+
return cnt
19+
for dy,dx in d:
20+
ny,nx=y+dy,x+dx
21+
if 0<=ny<4 and 0<=nx<4:
22+
if not visited[ny][nx]:
23+
visited[ny][nx]=True
24+
heapq.heappush(st,(cnt+1,ny,nx))
25+
if board[ny][nx] and (1<<board[ny][nx])&n:continue
26+
else:continue
27+
ny,nx = ny+dy,nx+dx
28+
if 0<=ny<4 and 0<=nx<4:
29+
if board[ny][nx] and (1<<board[ny][nx])&n:
30+
if not visited[ny][nx]:
31+
visited[ny][nx]=True
32+
heapq.heappush(st,(cnt+1,ny,nx))
33+
else:
34+
ny,nx=ny+dy,nx+dx
35+
if 0<=ny<4 and 0<=nx<4:
36+
if not visited[ny][nx]:
37+
visited[ny][nx]=True
38+
heapq.heappush(st,(cnt+1,ny,nx))
39+
else:
40+
heapq.heappush(st,(cnt+1,ny-dy,nx-dx))
41+
answer = 0
42+
def dp(y,x,n):
43+
if n==0:
1044
return 0
11-
if dp_arr[now][check]!=-1:return dp_arr[now][check]
12-
#check True -> 팀원들 맘대로
13-
#check False -> 팀원이 다안가면 팀중에 최소매출인원 무조건 보냄
14-
res = 0
15-
if check:
16-
for nx in adj[now]:
17-
res+=min(dp(nx,False),dp(nx,True)+sales[nx])
18-
else:
19-
flag = False
20-
tmp = sales[now]
21-
for nx in adj[now]:
22-
m1 = dp(nx,False)
23-
m2 = dp(nx,True)+sales[nx]
24-
if m2<=m1:
25-
flag=True
26-
res+=m2
27-
else:
28-
if m2-m1<tmp:
29-
tmp = m2-m1
30-
res+=m1
31-
if not flag:
32-
res+=tmp
33-
dp_arr[now][check]=res
45+
if dp_arr[y][x][n]!=-1:
46+
return dp_arr[y][x][n]
47+
res = INF
48+
for k,v in board_dict.items():
49+
if (1<<k) & n:
50+
y1,x1,y2,x2=v
51+
res = min(res,dp(y1,x1,n^(1<<k))+dfs(y,x,y2,x2,n)+dfs(y2,x2,y1,x1,n),dp(y2,x2,n^(1<<k))+dfs(y,x,y1,x1,n)+dfs(y1,x1,y2,x2,n))
52+
dp_arr[y][x][n]=res
3453
return res
35-
answer = dp(0,False)
54+
n=0
55+
for y,b in enumerate(board):
56+
for x,num in enumerate(b):
57+
if num:
58+
n=n|(1<<num)
59+
if num in board_dict:
60+
board_dict[num].extend([y,x])
61+
else:
62+
board_dict[num]=[y,x]
63+
answer = dp(r,c,n)
64+
answer += len(board_dict)*2
3665
return answer
3766

3867
r,c=map(int,input().split())

python/kakao/2021_7.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
def solution(sales, links):
5+
adj = [[] for _ in range(len(sales))]
6+
for a,b in links:
7+
adj[a-1].append(b-1)
8+
dp_arr = [[-1,-1] for _ in range(len(sales))]
9+
def dp(now,check):
10+
if len(adj[now])==0:
11+
return 0
12+
if dp_arr[now][check]!=-1:return dp_arr[now][check]
13+
#check True -> 팀원들 맘대로
14+
#check False -> 팀원이 다안가면 팀중에 최소매출인원 무조건 보냄
15+
res = 0
16+
if check:
17+
for nx in adj[now]:
18+
res+=min(dp(nx,False),dp(nx,True)+sales[nx])
19+
else:
20+
flag = False
21+
tmp = sales[now]
22+
for nx in adj[now]:
23+
m1 = dp(nx,False)
24+
m2 = dp(nx,True)+sales[nx]
25+
if m2<=m1:
26+
flag=True
27+
res+=m2
28+
else:
29+
if m2-m1<tmp:
30+
tmp = m2-m1
31+
res+=m1
32+
if not flag:
33+
res+=tmp
34+
dp_arr[now][check]=res
35+
return res
36+
answer = dp(0,False)
37+
return answer
38+
39+
r,c=map(int,input().split())
40+
41+
board = []
42+
tmp = []
43+
for idx,row in enumerate(list(map(str,input().split(',')))):
44+
row = row.replace("[","").replace("[","")
45+
row = row.replace("]","").replace("]","")
46+
if idx>1 and idx%4==0:
47+
board.append(tmp)
48+
tmp = [int(row)]
49+
else:
50+
tmp.append(int(row))
51+
board.append(tmp)
52+
print(solution(board,r,c))

0 commit comments

Comments
 (0)