File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ # [BOJ] 숩박꼭질 / 골드 4
3+ https://www.acmicpc.net/problem/12851
4+
5+ -문제
6+ goa) 수빈 -> 동생 위치까지 도달하는데 최단"시간" & 경우의 수 (미친?)
7+ # 조건
8+ i. 수빈의 위치 N , 동생 K (1차원)
9+ 2. 이동 방법 (현 위치:x ) => 3가지
10+ (1) 걷기 : x-1 , x+1
11+ (2) 순간이동 : 2*x
12+ - 유형: BFS
13+ field[x] = [[걸리는 시간,경우의 수]]
14+
15+ 5 17
16+ """
17+ import sys
18+ from collections import deque
19+ N , K = map (int , sys .stdin .readline ().split ())
20+ INF = 1000001
21+ field = [[INF ,0 ]]* INF # field[x] = 최단 시간
22+ # BFS 특 : 먼저 방문한 경우가 최단 시간임
23+ q = deque ([N ])
24+ field [N ] = [0 ,1 ] # 최단시간 , 경우의 수
25+
26+ while q :
27+ x = q .popleft () # 현 위치
28+ ct , cn = field [x ] # 현 위치에서 최단 시간 , 경우의 수
29+ # if field[K][0] < ct : # 종료 조건 :
30+ # break
31+ for nx in [x - 1 , x + 1 , 2 * x ]:
32+ if 0 <= nx < INF :
33+ #[1] 첫 방문인 경우 : field[nx][1] == 0 -> 최단 시간 업데이트 & q 에 넣기
34+ if field [nx ][1 ] == 0 :
35+ field [nx ] = [ct + 1 ,cn ]
36+ q .append (nx ) # 위치 업데이트
37+ # print(f"update {nx} => field[nx] : {field[nx]}")
38+ #[2] 중복 방문인 경우(최단 시간이 같을때)-> field[x][1]누적 경로의 수 추가
39+ elif field [nx ][0 ] == ct + 1 :
40+ field [nx ][1 ] += cn
41+ # print(f"## duplicate :{nx} - {field[nx]}")
42+ # 최단 시간이 더 큼 -> 암 것도 없음
43+
44+ print (field [K ][0 ])
45+ print (field [K ][1 ])
46+
You can’t perform that action at this time.
0 commit comments