일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
- BFS
- 브루트포스 알고리즘
- 알고리즘
- 그래프이론
- 그래프 탐색
- 그래프 이론
- BASIC
- 문자열
- 파이썬
- 프로그래머스스쿨
- 웹 프론트엔드
- CSS
- JavaScript
- 다이나믹 프로그래밍
- 자바스크립트
- 자료구조
- level2
- 그리디 알고리즘
- 너비 우선 탐색
- 딕셔너리
- 백준
- 정렬
- DP
- lv2
- DFS
- programmers
- 구현
- 프로그래머스
- 스택
- web
- Today
- Total
목록BFS (9)
DevLog:-)

문제 1.BFS코드 import sys from collections import deque x = int(sys.stdin.readline().strip()) def fun(start_x): q= deque() q.append((0,start_x)) while(q): C, cur_x =q.popleft() if(cur_x==1): return C elif(cur_x!=0): if(cur_x % 3 == 0): q.append((C+1,cur_x//3)) if(cur_x % 2 == 0): q.append((C+1,cur_x//2)) q.append((C+1,cur_x-1)) print(fun(x)) #큐에 연산 횟수, 연산 결괏값을 넣어 와일문을 돌려서 1이 나올 때의 연산 횟수 반환했다. 2.DP코드..

문제 코드 import sys from collections import deque n, k = map(int,sys.stdin.readline().strip().split()) def bfs(): q = deque() q.append(n) while q: v = q.popleft() if v == k: print(visited[v]) break for nv in (v - 1, v + 1, v * 2): if 0
문제 백준 1697-숨바꼭질 1697번: 숨바꼭질 수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 www.acmicpc.net 오류 숨바꼭질 문제 코드를 다음과 같이 풀었다. 결론은 시간초과로 인한 실패...ㅠㅠ 계속 보다가 다른 분들의 코들 봤는데 알고리즘이 유사해 원인을 찾기 어려워 ChatGPT에게 물어봤다....🌟 나: 백준 1697 숨바꼭질 문제를 푸는데 시간초과로 실패했어 내 코드를 보고 원인을 찾아줘 아래는 내가 제출한 코드야~ import sys from collections import deque n,k = map(..

문제 코드 설계 BFS와 DFS를 이용해 풀 수 있는 문제다 먼저 이중 for 문을 돌고 이때 1이면서 방문한 적이 없는 칸을 만나면 bsq 함수로 넘어가게 했다. 그리고 bsq를 돌면서 주변의 1을 다 탐색하게 했다. 1을 발견할 때마다 c 변수의 값을 올려 해당 단지에 속하는 집의 수를 카운트했다. bsq 함수가 하나 끝날 때마다 아파트 단지 한 개를 구했다는 것이기 때문에 단지의 수를 담는 변수인 count에 +1을 해줬다. 단지별 집의 수를 오름차순으로 정렬 출력해야 하기 때문에 bsq 함수에서 집의 수를 카운트했던 c를 함수 반환값으로 해서 바로 배열에 넣어 주었다.

문제 코드 import sys from collections import deque #60m r,c = map(int,sys.stdin.readline().strip().split()) graph = [] visited = [[False]*c for _ in range(r)] for _ in range(r): graph.append(list(map(int,sys.stdin.readline().strip()))) dx =[1,-1,0,0] dy =[0,0,1,-1] visited[0][0]=True q= deque() q.append((0,0,1)) f=0 while(q): cx,cy,count= q.popleft() for i in range(4): nx = cx+dx[i] ny = cy+dy[i] if..

문제 코드 import sys from collections import deque N,M,V = map(int,sys.stdin.readline().strip().split()) graph =[[] for _ in range(N+1)] def dfs(cur_v): visited.append(cur_v) for v in graph[cur_v]: if(v not in visited): dfs(v) return visited def bfs(start_v): visited=[start_v] q = deque() q.append(start_v) while(q): cur_v =q.popleft() for v in graph[cur_v]: if(v not in visited): visited.append(v) q...

문제코드BFSimport sysfrom collections import dequen = int(sys.stdin.readline())m = int(sys.stdin.readline())graph=[[] for _ in range(n+1)]def bfs(start_v): visited=[start_v] q =deque() q.append(start_v) while(q): cur_v=q.popleft() for v in graph[cur_v]: if(v not in visited): visited.append(v) q.append(v) re..

문제 Number of Islands - LeetCode Can you solve this real interview question? Number of Islands - Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent l leetcode.com 접근 방법 1.한 칸씩 확인한다.→ 이중for문 2.육지의 경우 주변을 다 탐색한다. → bfs or dfs 3.탐색한 육지는 다시 탐색하지 않도록 기록한다. →visite..

문제 소스코드 import sys from collections import deque n,b = map(int,sys.stdin.readline().split()) q= deque() q.append([n,1]) #현재 값과 레벨과 넣기 f=0 while(q): k=q[0] if(k[0]==b): #맨 앞에 칸이 b와 같은지 확인 print(k[1]) f=1 break else: q.popleft() #다를 경우 큐에서 빼내고 해당 값에서 파생된 값 갱신 if(k[0]*2