|
1 | 1 | import sys
|
2 | 2 | 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: |
10 | 44 | 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 |
34 | 53 | 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 |
36 | 65 | return answer
|
37 | 66 |
|
38 | 67 | r,c=map(int,input().split())
|
|
0 commit comments