한걸음

백준 2210 : 숫자판 점프 본문

Coding Test

백준 2210 : 숫자판 점프

우당탕탕 할 수 있다!!! 2023. 12. 6. 18:53
반응형

https://www.acmicpc.net/problem/2210

 

2210번: 숫자판 점프

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 이 가능한 경우들이다.

www.acmicpc.net

https://github.com/HPYoo/swcodingtest/commit/0a383076f742250287f370ff099159bef6acb04c

 

sw coding 2210 update(Baekjoon) · HPYoo/swcodingtest@0a38307

Showing 1 changed file with 28 additions and 0 deletions.

github.com

1. 배열 사이즈가 작아 BFS로도 풀이가 가능

 BFS사용에 익숙해지니까 왠만한 문제를 BFS로 해결한다.

큐에 넣고 반복문 돌릴 때 내가 만들어둔 숫자 조합 리스트에서 비교하고, 없으면 추가하며 큐가 점차 빠지도록 만들어주면 된다.

 

2. 전체코드

메모리 : 115164 KB, 시간 : 232 ms, 풀이 시간 : 15min

board = [list(map(int, input().split())) for _ in range(5)]

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def bfs():
    arr = []
    for i in range(5):
        for j in range(5):
            q = [[i, j, 1, str(board[i][j])]]
            while q :
                x, y, cnt, string = q[0]
                del q[0]
                if cnt == 6 :
                    if string in arr :
                        continue
                    else :
                        arr.append(string)
                        continue
                for d in range(4):
                    nx = x + dx[d]
                    ny = y + dy[d]
                    if 0 <= nx < 5 and 0 <= ny < 5 :
                        q.append([nx, ny, cnt + 1, string + str(board[nx][ny])])
    return arr

result = bfs()
print(len(result))

 

반응형

'Coding Test' 카테고리의 다른 글

백준 23291 : 어항 정리  (3) 2023.12.18
백준 1941 : 소문난 칠공주(Python)  (2) 2023.12.11
백준 1986 : 체스  (1) 2023.12.05
백준 7562 : 나이트의 이동  (1) 2023.12.04
백준 2234 : 성곽  (3) 2023.11.29