Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

Life Engineering

[BOJ 11060] 점프 점프 (Python3) 본문

Problem Solving

[BOJ 11060] 점프 점프 (Python3)

흑개 2021. 2. 15. 23:55

www.acmicpc.net/problem/11060

 

11060번: 점프 점프

재환이가 1×N 크기의 미로에 갇혀있다. 미로는 1×1 크기의 칸으로 이루어져 있고, 각 칸에는 정수가 하나 쓰여 있다. i번째 칸에 쓰여 있는 수를 Ai라고 했을 때, 재환이는 Ai이하만큼 오른쪽으로

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from collections import deque
 
N=int(input())
graph=list(map(int, input().split()))
visited=[0]*N
 
def bfs():
    queue=deque()
    queue.append(0)
    while queue:
        x=queue.popleft()
        for i in range(1,graph[x]+1):
            nx=x+i
            if 0<=nx<and visited[nx]==0:
                visited[nx]=visited[x]+1
                queue.append(nx)
if N==1:
    print(0)
else:
    bfs()
    if visited[N-1]==0:
        print(-1)
    else:
        print(visited[N-1])
cs

 

BFS 로 풀었다. 방문하고, 후보 방문지들 큐에 넣고, visited 배열에 후보 방문지들 방문할 때마다 +1을 추가하여 준다.

결과적으로 visited[목적지]에 최소 점프 수가 나올 수 있다.

검색해보니 DP 로도 많이 푼다.

 

개인적으로 BOJ 1697번과 유사해서 쉽게 풀 수 있었다.

www.acmicpc.net/problem/1697

 

1697번: 숨바꼭질

수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from collections import deque
 
n,k=map(int, input().split())
MAX=10**5
dist=[0]*(MAX+1)
 
def bfs(n,k):
    queue=deque([n])
    if n==k:
        print(0)
    else:
        while queue:
            x=queue.popleft()
            if x-1==or x+1==or x*2==k:
                print(dist[x]+1)
                break
            for i in (x-1, x+1, x*2):
                if i>100000 or i<0 or dist[i]!=0#이미 방문한것이나 범위를 벗어난것은 탐색하지않음
                    continue
                queue.append(i)     #처음 방문하는 경우에만 붙임
                dist[i]=dist[x]+1
 
bfs(n,k)
cs

'Problem Solving' 카테고리의 다른 글

[BOJ 6603] 로또 (Python3)  (0) 2021.02.17
[코딩테스트 대비 커리큘럼]  (0) 2021.02.17
[BOJ 15686] 치킨 배달 (Python3)  (0) 2021.02.15
[BOJ 14502] 연구소 (Python3)  (0) 2021.02.15
[BOJ 1182] 부분수열의 합 (Python3)  (0) 2021.02.14