Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 삼성SDS Brightics
- Brightics를 이용한 분석
- 포스코 청년
- 삼성SDS Brigthics
- Brightics Studio
- 브라이틱스
- 직원 이직률
- 모델링
- Brightics
- 혼공학습단
- 혼공머신러닝딥러닝
- 영상제작기
- 팀 분석
- 브라이틱스 서포터즈
- 추천시스템
- 개인 의료비 예측
- 혼공
- 삼성SDS
- 노코드AI
- Brigthics를 이용한 분석
- 데이터 분석
- Brigthics
- 데이터분석
- Brigthics Studio
- 혼공머신
- 포스코 아카데미
- 삼성 SDS
- 직원 이직여부
- 캐글
- 삼성 SDS Brigthics
Archives
- Today
- Total
데이터사이언스 기록기📚
[백준/Python] 2210번(DFS, 브루트포스)_숫자판 점프 본문
📌문제 유형
그래프 이론, 브루트포스 알고리즘, 그래프 탐색, DFS(실버2)
📌문제
2210번: 숫자판 점프
111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 이 가능한 경우들이다.
www.acmicpc.net
📌나의 문제풀이
- 실패
graph = []
num_list = []
for i in range(5):
graph.append(list(map(int,input().split())))
def dfs(graph,cnt,ans,x,y):
dx = [0,0,1,-1]
dy = [1,-1,0,0]
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
# 여기서 오류
cnt += 1
if nx > 0 and nx < len(graph) and ny > 0 and ny < len(graph[0]):
ans += str(graph[nx][ny])
if len(ans) == 6 and cnt == 6:
if ans not in num_list:
num_list.append(ans)
return
if cnt < 6:
dfs(graph,cnt,ans,nx,ny)
else:
return
for i in range(len(graph)):
for j in range(len(graph[0])):
dfs(graph,0,'',i,j)
print(num_list)
print(len(num_list))
- 다른사람 코드 참고하여 성공
graph = []
num_list = []
for i in range(5):
graph.append(list(map(int,input().split())))
def dfs(graph,ans,x,y):
if len(ans) == 6:
if ans not in num_list:
num_list.append(ans)
return
dx = [0,0,1,-1]
dy = [1,-1,0,0]
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < 5 and 0 <= ny < 5:
dfs(graph,ans + str(graph[nx][ny]),nx,ny)
for i in range(5):
for j in range(5):
dfs(graph,'',i,j)
print(len(num_list))
📌 다른사람의 문제풀이
#380 백준 파이썬 [2210] 숫자판 점프 - DFS
https://www.acmicpc.net/problem/2210 SOLUTION 0,0 부터 4,4 까지 모든 출발점에 대하여 DFS(깊이우선탐색)을 실행시켜주는 문제다. 들어오는 입력과 숫자들을 모두 str취급해주어(0또한 숫자로 취급되기에) 6글
claude-u.tistory.com
def dfs(x, y, number):
if len(number) == 6: #6자리 숫자가 만들어졌다면
if number not in result: #result에 없다면
result.append(number)
return
dx = [1, -1, 0, 0] #상하좌우 확인 x
dy = [0, 0, 1, -1] #상하좌우 확인 y
for k in range(4):
ddx = x + dx[k]
ddy = y + dy[k]
if 0 <= ddx < 5 and 0 <= ddy < 5: #범위 내에 있다면
dfs(ddx, ddy, number + matrix[ddx][ddy]) #6글자가 될 때 까지 재귀
#입력
matrix = [list(map(str, input().split())) for _ in range(5)]
result = []
for i in range(5):
for j in range(5):
dfs(i, j, matrix[i][j]) #0,0부터 4,4까지 모두 검사
print(len(result))
📌 리뷰
- Q1) 문자 길이 6 확인하는 과정이 좌표변환 과정 안에 있으면?
- for문 안에서 함수가 끝나는 거라, 다른 for문 값을 확인 안하고 건너 뛸 수 있음
- DFS 종료하는 과정은, 함수 초입에 검사해주기!!!
- Q2) 좌표 범위가 5안에 있고, ans += str(graph[nx][ny]) dfs(graph, ans, nx,ny)를 하면 회귀 오류가 되는 경우는?
- ans += str(graph[nx][ny]) 해당 형태로 넘겨주면 같은 depth에서 숫자 갱신이 안되서 오류남
- 다시 돌아올 수 있는 경우, ans + str(graph[nx][ny]) 값 자체를 다 넘겨주기(갱신 안되는 형태로!)
[leet22] DFS 인자를 할당된 객체가 아닌 직접 지정해줘야 하는 경우
Parentheses, DFS(재귀함수에 직접 값 입력하기)
velog.io
# 오류
if 0 <= nx < 5 and 0 <= ny < 5:
ans += str(graph[nx][ny])
dfs(graph,ans,nx,ny)
# 오류 아님
if 0 <= nx < 5 and 0 <= ny < 5:
dfs(graph,ans + str(graph[nx][ny]),nx,ny)
728x90
'Coding Test > 백준(Python)' 카테고리의 다른 글
[백준/Python] 14248번(BFS)_점프 점프 (0) | 2023.04.20 |
---|---|
[백준/Python] 13699번(DP)_점화식 (0) | 2023.04.19 |
[백준/Python] 1018번(브루트포스 알고리즘)_체스판 다시 칠하기 (0) | 2023.04.18 |
[백준/Python] 9655번(수학, DP, 게임이론)_돌 게임 (0) | 2023.04.15 |
[백준/Python] 1758번(그리디, 정렬)_알바생 강호 (0) | 2023.04.15 |
Comments