Baekjoon/백트래킹

Baekjoon/백트래킹

[백준] 14888 연산자 끼워넣기 (실버1) / 백트래킹

문제요약 나의 코드 및 설명 def dfs(count, result): if count == len(num_list)-1: ans.append(result) return for i in range(len(op)): #연산을 하나하나 방문하여 돌린다. if op[i] != 0: #연산자를 사용할 수 있다면, op[i] -= 1 #방문한 연산자의 사용 가능 횟수를 1만큼 줄인다. if i == 0: dfs(count+1, result + num_list[count+1]) elif i == 1: dfs(count+1, result - num_list[count+1]) elif i == 2: dfs(count+1, result * num_list[count+1]) elif i == 3: if result < 0..

Baekjoon/백트래킹

[백준] 16943 숫자 재배치 (실버1) / 백트래킹

문제요약 나의 코드 및 설명 01 def dfs(n, temp_list): global max_ans if n == len(a_list): #a의 길이만큼 숫자를 뽑았다면 #리스트에 저장된 숫자들을 정수로 바꾸고 다시 리스트에 저장(맨 앞이 0이라면, 리스트 길이가 줄어들 것) #다시 저장한 리스트의 길이가 a의 길이와 차이가 있다면 앞의 숫자가 0이 왔다는 뜻 if len(list(map(int,str(int("".join(map(str, temp_list))))))) != len(a_list): #그 즉시리턴한다. return else: #만약 그렇지 않다면 최대값을 구한다 if int("".join(map(str, temp_list))) < b: if max_ans < int("".join(map(..

Baekjoon/백트래킹

[백준] 1189 컴백홈 (실버1) / 백트래킹

문제요약 나의 코드 및 설명 def dfs(x,y,cnt): global ans #그리드 상 오른쪽 위에 도착하면 if x == c-1 and y == 0: if cnt == k: #만약 거리가 k이면 ans += 1 #ans에 1 추가 return #네 방향에 대해 다음 좌표로 이동할 수 있는지 확인 for i in range(4): nx = x + dx[i] ny = y + dy[i] if nx=r: continue else: #다음 좌표를 아직 방문 안했고, '.'이라면 다음 좌표 방문 if v[ny][nx] == 0 and board[ny][nx] == '.': v[ny][nx] = 1 dfs(nx,ny,cnt+1) #다음 좌표 방문했으니 cnt+1. 이것은 거리가 된다. v[ny][nx] = 0..

Baekjoon/백트래킹

[백준] 2992 크면서 작은 수 (실버3) / 문자열, 백트래킹

문제요약 나의 코드 및 설명 def dfs(count, temp_list): global min_num, ans if count == len(x_list): #만약 모든 숫자를 다 사용했을 때, if x

Baekjoon/백트래킹

[백준] 1476 날짜 계산 (실버5) / 백트래킹

문제요약 나의 코드 및 설명 01 - DFS import sys sys.setrecursionlimit(10**6) def dfs(x,y,z, count): global ans if x == 16: x = 1 if y == 29: y = 1 if z == 20: z = 1 if x == e and y == s and m == z: ans = count return dfs(x+1, y+1, z+1, count+1) e,s,m = map(int, input().split()) ans = 0 dfs(1,1,1,1) print(ans) 나의 코드 및 설명 02- while문 e,s,m = map(int, input().split()) a,b,c = 1,1,1 ans = 1 while True: if a == e..

Baekjoon/백트래킹

[백준] 2529 부등호 (실버1) / 백트래킹

문제요약 나의 코드 및 설명 def dfs(start, idx, temp_list): global max_result,min_result,max_ans,min_ans if idx == k: #idx:부등호를 저장한 리스트 #최소 문자열이라면 최소 문자열을 갱신 후 리턴 if min_result > int("".join(map(str,temp_list))): min_result = int("".join(map(str,temp_list))) min_ans = "".join(map(str,temp_list)) #최대 문자열이라면 최대 문자열을 갱신 후 리턴 if max_result < int("".join(map(str,temp_list))): max_result = int("".join(map(str,tem..

Baekjoon/백트래킹

[백준] 18429 근손실 (실버3) / 백트래킹

문제요약 나의 코드 및 설명 01 (변수로 넘겨줄 시) #변수로 넘겨줄 시 def dfs(count, muscle): global ans #중량이 500이하 되면 리턴 if muscle < 500: return #키트를 다 선택했다면 ans +=1 if count == n: ans+=1 return for i in range(len(kit)): #중복된 키트를 사용하지 않기 위해 방문 처리 if visited[i] == 0: visited[i] = 1 #키트 사용 시 중량을 같이 넘겨준다. dfs(count+1, muscle+kit[i]-k) #돌아와서는 방문처리 해제 visited[i] = 0 n,k = map(int, input().split()) kit = list(map(int, input().s..

Baekjoon/백트래킹

[백준] 6603 로또 (실버2) / 백트래킹

문제요약 나의 코드 및 설명 def dfs(n, start, temp_list): if len(temp_list) == 6: #전달받은 임시 리스트 길이가 6이라면 result.append(temp_list) #추가 return if n == count: return for i in range(start, count): dfs(n+1, i+1, temp_list + [num_list[i]]) while True: num_list = list(map(int, input().split())) if num_list == [0]: #0을 입력받는다면 반복문 탈출 break count = num_list.pop(0) result = [] dfs(0,0,[]) for i in result: print(" ".joi..

hellosonic
'Baekjoon/백트래킹' 카테고리의 글 목록