From 693b65680c1db98a6dec9b366317dc46ab94279a Mon Sep 17 00:00:00 2001 From: caffrose415 Date: Wed, 23 Aug 2023 22:13:23 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[DFS=5FBFS]=20=ED=83=80=EA=B2=9F=EB=84=98?= =?UTF-8?q?=EB=B2=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\204_\353\205\270\354\234\240\353\257\274.py" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 "Programmers/DFS_BFS/\355\203\200\352\262\237\353\204\230\353\262\204_\353\205\270\354\234\240\353\257\274.py" diff --git "a/Programmers/DFS_BFS/\355\203\200\352\262\237\353\204\230\353\262\204_\353\205\270\354\234\240\353\257\274.py" "b/Programmers/DFS_BFS/\355\203\200\352\262\237\353\204\230\353\262\204_\353\205\270\354\234\240\353\257\274.py" new file mode 100644 index 00000000..20f4a4a8 --- /dev/null +++ "b/Programmers/DFS_BFS/\355\203\200\352\262\237\353\204\230\353\262\204_\353\205\270\354\234\240\353\257\274.py" @@ -0,0 +1,15 @@ +def solution(numbers, target): + n = len(numbers) + answer = 0 + + def dfs(idx, result): + if idx == n: + if result == target: + nonlocal answer + answer += 1 + return + else: + dfs(idx+1, result+numbers[idx]) # 그 다음값을 더하고 빼고 + dfs(idx+1, result-numbers[idx]) + dfs(0, 0) + return answer From 1e1e450b529a3c55d668b291465a33e2a6ea6cbb Mon Sep 17 00:00:00 2001 From: caffrose415 Date: Sat, 26 Aug 2023 11:52:46 +0900 Subject: [PATCH 2/5] =?UTF-8?q?[DFS=5FBFS]=20=EB=84=A4=ED=8A=B8=EC=9B=8C?= =?UTF-8?q?=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\254_\353\205\270\354\234\240\353\257\274.py" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 "Programmers/DFS_BFS/\353\204\244\355\212\270\354\233\214\355\201\254_\353\205\270\354\234\240\353\257\274.py" diff --git "a/Programmers/DFS_BFS/\353\204\244\355\212\270\354\233\214\355\201\254_\353\205\270\354\234\240\353\257\274.py" "b/Programmers/DFS_BFS/\353\204\244\355\212\270\354\233\214\355\201\254_\353\205\270\354\234\240\353\257\274.py" new file mode 100644 index 00000000..f6ca9054 --- /dev/null +++ "b/Programmers/DFS_BFS/\353\204\244\355\212\270\354\233\214\355\201\254_\353\205\270\354\234\240\353\257\274.py" @@ -0,0 +1,16 @@ +def solution(n, computers): + answer = 0 + visited = [False for i in range(n)] + for i in range(n): + if visited[i] == False: + DFS(n, computers, i, visited) + answer += 1 + return answer + + +def DFS(n, computers, i, visited): + visited[i] = True + for j in range(n): + if j != i and computers[i][j] == 1: + if visited[j] == False: + DFS(n, computers, j, visited) From 8e571ed3670d74c92ca04d49e7690b1a91b8db2a Mon Sep 17 00:00:00 2001 From: caffrose415 Date: Sat, 26 Aug 2023 15:19:58 +0900 Subject: [PATCH 3/5] =?UTF-8?q?[DFS=5FBFS]=20=EA=B2=8C=EC=9E=84=EB=A7=B5?= =?UTF-8?q?=EC=B5=9C=EB=8B=A8=EA=B1=B0=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4_\353\205\270\354\234\240\353\257\274.py" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "Programmers/DFS_BFS/\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254_\353\205\270\354\234\240\353\257\274.py" diff --git "a/Programmers/DFS_BFS/\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254_\353\205\270\354\234\240\353\257\274.py" "b/Programmers/DFS_BFS/\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254_\353\205\270\354\234\240\353\257\274.py" new file mode 100644 index 00000000..dc4a5dfa --- /dev/null +++ "b/Programmers/DFS_BFS/\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254_\353\205\270\354\234\240\353\257\274.py" @@ -0,0 +1,33 @@ +from collections import deque + +dx = [0, 0, -1, 1] +dy = [-1, 1, 0, 0] + + +def BFS(x, y, visited, maps, n, m): + visited[x][y] = True + q = deque() + q.append((x, y)) + + while q: + x, y = q.popleft() + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if nx < 0 or nx >= n or ny < 0 or ny >= m: + continue + if maps[nx][ny] != 0 and not visited[nx][ny]: + visited[nx][ny] = True + q.append((nx, ny)) + maps[nx][ny] = maps[x][y] + 1 + return maps[-1][-1] if maps[-1][-1] > 1 else -1 + + +def solution(maps): + answer = 0 + n = len(maps) + m = len(maps[0]) + visited = [[False] * m for _ in range(n)] + answer = BFS(0, 0, visited, maps, n, m) + + return answer From 32697671ae8e041881900068e838e61c1b1191dc Mon Sep 17 00:00:00 2001 From: caffrose415 Date: Mon, 28 Aug 2023 22:48:04 +0900 Subject: [PATCH 4/5] =?UTF-8?q?[DFS=5FBFS]=20=EB=8B=A8=EC=96=B4=EB=B3=80?= =?UTF-8?q?=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0_\353\205\270\354\234\240\353\257\274.py" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "Programmers/DFS_BFS/\353\213\250\354\226\264\353\263\200\355\231\230_\353\205\270\354\234\240\353\257\274.py" diff --git "a/Programmers/DFS_BFS/\353\213\250\354\226\264\353\263\200\355\231\230_\353\205\270\354\234\240\353\257\274.py" "b/Programmers/DFS_BFS/\353\213\250\354\226\264\353\263\200\355\231\230_\353\205\270\354\234\240\353\257\274.py" new file mode 100644 index 00000000..b9272f53 --- /dev/null +++ "b/Programmers/DFS_BFS/\353\213\250\354\226\264\353\263\200\355\231\230_\353\205\270\354\234\240\353\257\274.py" @@ -0,0 +1,28 @@ +from collections import deque + + +def solution(begin, target, words): + answer = 0 + visited = [False for _ in range(len(words))] + q = deque() + q.append((begin, 0)) + + while q: + word, count = q.popleft() + if word == target: + answer = count + return answer + else: + for i in range(len(words)): + tempCount = 0 + if visited[i] != True: + for j in range(len(word)): + if word[j] != words[i][j]: + tempCount += 1 + + if tempCount == 1: + count += 1 + q.append((words[i], count)) + visited[i] = True + + return answer From 6f51b39240e7159c01b230081d8e1946cab1feb7 Mon Sep 17 00:00:00 2001 From: caffrose415 Date: Tue, 29 Aug 2023 00:07:17 +0900 Subject: [PATCH 5/5] =?UTF-8?q?[DFS=5FBFS]=20=EC=97=AC=ED=96=89=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4_\353\205\270\354\234\240\353\257\274.py" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "Programmers/DFS_BFS/\354\227\254\355\226\211\352\262\275\353\241\234_\353\205\270\354\234\240\353\257\274.py" diff --git "a/Programmers/DFS_BFS/\354\227\254\355\226\211\352\262\275\353\241\234_\353\205\270\354\234\240\353\257\274.py" "b/Programmers/DFS_BFS/\354\227\254\355\226\211\352\262\275\353\241\234_\353\205\270\354\234\240\353\257\274.py" new file mode 100644 index 00000000..9b3a9395 --- /dev/null +++ "b/Programmers/DFS_BFS/\354\227\254\355\226\211\352\262\275\353\241\234_\353\205\270\354\234\240\353\257\274.py" @@ -0,0 +1,50 @@ +from collections import deque +# 테케 1,2번 실패 +# def solution(tickets): +# answer = [] +# q=deque() +# tickets.sort(key=lambda x:x[1]) +# visited=[False for _ in range(len(tickets))] +# for i in range(len(tickets)): +# if tickets[i][0]=='ICN': +# q.append((tickets[i][0],tickets[i][1])) +# visited[i]=True +# break + +# while q: +# start,end =q.popleft() +# answer.append(start) +# for i in range(len(tickets)): +# if tickets[i][0]==end and visited[i]==False: +# q.append((tickets[i][0],tickets[i][1])) +# visited[i]=True +# break + +# answer.append(end) +# return answer + + +def solution(tickets): + answer = [] + graph = {} + + for start, end in tickets: + if start in graph: + graph[start].append(end) + else: + graph[start] = [end] + + for key in graph.keys(): + graph[key].sort(reverse=True) + + q = deque(["ICN"]) + + while q: + current = q[-1] + if current in graph and graph[current]: + q.append(graph[current].pop()) + else: + # popleft해서 그냥 담는게 안되는 예시 [["ICN","A"],["ICN","B"],["B","ICN"]] + answer.append(q.pop()) + answer.reverse() + return answer