Baekjoon

[BOJ / 백준] 2210번 숫자판 점프 파이썬(Python) 문제 풀이

728x90

 

문제

 

문제 링크 : 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

 

 

 

 

 

CODE

import sys
from itertools import product

input = sys.stdin.readline

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

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

for i in range(5):
    for j in range(5):
        for dir in product(range(4), repeat=5):         # 이동방향 모든 경우의 수
            temp = []
            temp.append(num[i][j])
            x, y = i, j
            for k in dir:
                x, y = x + dx[k], y + dy[k]
                if not (0 <= x < 5 and 0 <= y < 5):     # 숫자판 범위를 벗어난 경우 중단
                    break
                temp.append(num[x][y])
            if len(temp) == 6:
                result.append("".join(map(str, temp)))  # 문자열로 합쳐서 저장

print(len(set(result)))     # 중복을 없애고 개수 출력

 

 

 

 

 

풀이

💡 idea

- 각 위치에서 5방향으로 이동하는 모든 경우의 수에 대해 이동하면서 거친 여섯 자리 숫자 구하기

 

 

💡 implementation

- 0~4의 방향을 중복 가능하게 5개 골라야 하기 때문에 itertools의 product를 사용

for dir in product(range(4), repeat=5):

 

- "".join을 사용할 때, temp의 원소들이 숫자이기 때문에 map을 사용하여 문자열로 변환한 뒤 join하였다

result.append("".join(map(str, temp)))

 

- 집합은 중복이 없다는 성질을 이용한 set()을 사용하여 리스트에서 중복 없애기

print(len(set(result)))

 

 

 

 

 

결과

 

 

 

 

 

 

 

728x90