From d19e5129eface82ff5acced02ab64d22956328e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=A1=EB=AF=BC=EC=A7=84?= Date: Mon, 28 Aug 2023 18:55:59 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=EB=84=A4=ED=8A=B8=EC=9B=8C=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4_\354\206\241\353\257\274\354\247\204.py" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 "Programmers/DFS_BFS/\353\204\244\355\212\270\354\233\214\355\201\254_\354\206\241\353\257\274\354\247\204.py" diff --git "a/Programmers/DFS_BFS/\353\204\244\355\212\270\354\233\214\355\201\254_\354\206\241\353\257\274\354\247\204.py" "b/Programmers/DFS_BFS/\353\204\244\355\212\270\354\233\214\355\201\254_\354\206\241\353\257\274\354\247\204.py" new file mode 100644 index 00000000..fe70e601 --- /dev/null +++ "b/Programmers/DFS_BFS/\353\204\244\355\212\270\354\233\214\355\201\254_\354\206\241\353\257\274\354\247\204.py" @@ -0,0 +1,20 @@ +from collections import deque + +def solution(n, computers): + answer = 0 + global visited + visited = [False] * n + for i in range(n): + if not visited[i]: + bfs(i, n, computers) + answer += 1 + return answer + +def bfs(start, n, computers): + q = deque([start]) + while q: + com1 = q.popleft() + for com2 in range(n): + if computers[com1][com2] == 1 and not visited[com2]: + visited[com2] = True + q.append(com2) \ No newline at end of file From 72240d4be0af7b9b467b7b68ef62f3099b3b380b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=A1=EB=AF=BC=EC=A7=84?= Date: Mon, 28 Aug 2023 19:07:30 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EA=B2=8C=EC=9E=84=20=EB=A7=B5=20=EC=B5=9C?= =?UTF-8?q?=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_\354\206\241\353\257\274\354\247\204.py" | 21 +++++++++++++++++++ 1 file changed, 21 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_\354\206\241\353\257\274\354\247\204.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_\354\206\241\353\257\274\354\247\204.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_\354\206\241\353\257\274\354\247\204.py" new file mode 100644 index 00000000..196c3791 --- /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_\354\206\241\353\257\274\354\247\204.py" @@ -0,0 +1,21 @@ +from collections import deque + +def solution(maps): + bfs((0, 0), maps) + if maps[len(maps)-1][len(maps[0])-1] == 1: + return -1 + return maps[len(maps)-1][len(maps[0])-1] + +def bfs(start, maps): + q = deque([start]) + dx = [0, 0, -1, 1] + dy = [-1, 1, 0, 0] + while q: + x, y = q.popleft() + for i in range(4): + nx, ny = x + dx[i], y + dy[i] + if 0 <= nx < len(maps) and 0 <= ny < len(maps[0]) and maps[nx][ny] == 1: + maps[nx][ny] = maps[x][y] + 1 + q.append((nx, ny)) + +print(solution([[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]])) \ No newline at end of file From 3146e89ae535f46a9749d550c2ade2dce36d402e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=A1=EB=AF=BC=EC=A7=84?= Date: Mon, 28 Aug 2023 19:49:21 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=EB=8B=A8=EC=96=B4=EB=B3=80=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0_\354\206\241\353\257\274\354\247\204.py" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "Programmers/DFS_BFS/\353\213\250\354\226\264\353\263\200\355\231\230_\354\206\241\353\257\274\354\247\204.py" diff --git "a/Programmers/DFS_BFS/\353\213\250\354\226\264\353\263\200\355\231\230_\354\206\241\353\257\274\354\247\204.py" "b/Programmers/DFS_BFS/\353\213\250\354\226\264\353\263\200\355\231\230_\354\206\241\353\257\274\354\247\204.py" new file mode 100644 index 00000000..6f94d7ff --- /dev/null +++ "b/Programmers/DFS_BFS/\353\213\250\354\226\264\353\263\200\355\231\230_\354\206\241\353\257\274\354\247\204.py" @@ -0,0 +1,37 @@ +from collections import deque + + +def solution(begin, target, words): + if target not in words: + return 0 + + words.append(begin) + words_dict = {word: [] for word in words} + + for i in range(len(words)): + for j in range(i + 1, len(words)): + cnt = 0 + for idx in range(len(words[i])): + if words[i][idx] != words[j][idx]: + cnt += 1 + if cnt == 1: + words_dict[words[i]].append(words[j]) + words_dict[words[j]].append(words[i]) + + + q = deque([(begin, 0)]) + visited = [] + while q: + now, cnt = q.popleft() + if target in words_dict[now]: + return cnt + 1 + elif words_dict[now]: + for w in words_dict[now]: + if w not in visited: + q.append((w, cnt + 1)) + visited.append(w) + return 0 + +print(solution("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"])) +print(solution("hit", "cog", ["hot", "dot", "dog", "lot", "log"])) +print(solution("aab", "aba", ["abb", "aba"])) \ No newline at end of file From 80ea9e14664c1c73167a6f2760fb28539130bd88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=A1=EB=AF=BC=EC=A7=84?= Date: Mon, 28 Aug 2023 21:11:16 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=EC=97=AC=ED=96=89=EA=B2=BD=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4_\354\206\241\353\257\274\354\247\204.py" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 "Programmers/DFS_BFS/\354\227\254\355\226\211\352\262\275\353\241\234_\354\206\241\353\257\274\354\247\204.py" diff --git "a/Programmers/DFS_BFS/\354\227\254\355\226\211\352\262\275\353\241\234_\354\206\241\353\257\274\354\247\204.py" "b/Programmers/DFS_BFS/\354\227\254\355\226\211\352\262\275\353\241\234_\354\206\241\353\257\274\354\247\204.py" new file mode 100644 index 00000000..ae0092a5 --- /dev/null +++ "b/Programmers/DFS_BFS/\354\227\254\355\226\211\352\262\275\353\241\234_\354\206\241\353\257\274\354\247\204.py" @@ -0,0 +1,21 @@ +def solution(tickets): + global answer + answer = [] + visited = [False] * len(tickets) + + dfs("ICN", ["ICN"], visited, tickets) + answer.sort() + return answer[0] + + +def dfs(dept, arr, visited, tickets): + if len(arr) == len(tickets)+1: + answer.append(arr) + + for i in range(len(tickets)): + if tickets[i][0] == dept and not visited[i]: + visited[i] = True + dfs(tickets[i][1], arr+[tickets[i][1]], visited, tickets) + visited[i] = False + +print(solution([["ICN", "JFK"], ["HND", "IAD"], ["JFK", "HND"]])) \ No newline at end of file From 39d8c2222754eb1477bce1b1555424e98447f928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=A1=EB=AF=BC=EC=A7=84?= Date: Mon, 28 Aug 2023 21:27:22 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=ED=83=80=EA=B2=9F=EB=84=98=EB=B2=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\204_\354\206\241\353\257\274\354\247\204.py" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 "Programmers/DFS_BFS/\355\203\200\352\262\237\353\204\230\353\262\204_\354\206\241\353\257\274\354\247\204.py" diff --git "a/Programmers/DFS_BFS/\355\203\200\352\262\237\353\204\230\353\262\204_\354\206\241\353\257\274\354\247\204.py" "b/Programmers/DFS_BFS/\355\203\200\352\262\237\353\204\230\353\262\204_\354\206\241\353\257\274\354\247\204.py" new file mode 100644 index 00000000..e5fd8c75 --- /dev/null +++ "b/Programmers/DFS_BFS/\355\203\200\352\262\237\353\204\230\353\262\204_\354\206\241\353\257\274\354\247\204.py" @@ -0,0 +1,16 @@ +def solution(numbers, target): + global answer + answer = 0 + dfs(0, numbers, 0, target) + return answer + +def dfs(depth, numbers, tmp, target): + global answer + + if depth == len(numbers): + if tmp == target: + answer += 1 + return + + dfs(depth+1, numbers, tmp+numbers[depth], target) + dfs(depth+1, numbers, tmp-numbers[depth], target) \ No newline at end of file