본문 바로가기

bfs14

[백준|파이썬] 5014: 스타트링크 (실버1) 문제 https://www.acmicpc.net/problem/5014 5014번: 스타트링크 첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다. www.acmicpc.net 🐍파이썬 import sys from collections import deque def bfs(x): queue = deque([x]) tower[x] = 0 while queue: x = queue.popleft() if x == g: return tower[x] for nx in [x+u, x-d]: if nx > f or nx 2023. 4. 12.
[백준|파이썬] 1697: 숨바꼭질 (실버1) 문제 https://www.acmicpc.net/problem/1697 1697번: 숨바꼭질 수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 www.acmicpc.net 🐍파이썬 import sys from collections import deque n, k = map(int, sys.stdin.readline().split()) #문제에서 주어진 n, k 값의 범위를 활용해 방문여부를 저장하는 리스트를 생성 sumba = [0 for _ in range(100001)] def bfs(x): queue = deque([x]) wh.. 2023. 4. 12.
[백준|파이썬] 7569: 토마토 (골드5) 문제 https://www.acmicpc.net/problem/7569 7569번: 토마토 첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100, www.acmicpc.net 🐍파이썬 import sys from collections import deque def bfs(): while queue: #익은 토마토의 좌표를 기반으로 bfs 탐색 x, y, z = queue.popleft() for i in range(6): nx = x + dx[i] ny = y + dy[i] nz = z + dz[i] if nx >= m or ny >= n .. 2023. 4. 12.
[백준|파이썬] 13700: 완전 범죄 (실버1) 문제 https://www.acmicpc.net/problem/13700 13700번: 완전 범죄 첫째 줄에 N, S, D, F, B, K가 주어지고, K > 0인 경우에는 둘째 줄에 경찰서의 위치 l1, l2, …, lK가 주어진다. (1 ≤ S, D ≤ N ≤ 100000, 0 ≤ F, B ≤ 100000, 0 ≤ K ≤ N/2, S ≠ D ≠ l) www.acmicpc.net 🐍파이썬 import sys from collections import deque def bfs(cnt, x): queue = deque() queue.append((cnt, x))#(이동횟수, 좌표x) while queue: cnt, x = queue.popleft() dx = [x-b, x+f]#뒤로 이동한 좌표, 앞으로 .. 2023. 4. 11.
[백준|파이썬] 21736: 헌내기는 친구가 필요해(DFS/BFS 풀이) (실버2) 문제 https://www.acmicpc.net/problem/21736 21736번: 헌내기는 친구가 필요해 2020년에 입학한 헌내기 도연이가 있다. 도연이는 비대면 수업 때문에 학교에 가지 못해 학교에 아는 친구가 없었다. 드디어 대면 수업을 하게 된 도연이는 어서 캠퍼스 내의 사람들과 친해지고 www.acmicpc.net 🐍파이썬 BFS import sys from collections import deque def bfs(x, y): global cnt queue = deque() queue.append((x, y)) sch[x][y] = 'X'#방문처리 while queue: x, y = queue.popleft() for i in range(4): nx = x + dx[i] ny = y + .. 2023. 4. 11.
[백준|파이썬] 2667: 단지번호붙이기(DFS/BFS 풀이) (실버1) 문제 https://www.acmicpc.net/problem/2667 2667번: 단지번호붙이기 과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여 www.acmicpc.net 🐍파이썬 DFS import sys sys.setrecursionlimit(10**6) n = int(sys.stdin.readline()) house = [] danzi = [] for _ in range(n): house.append(list(map(int, sys.stdin.readline().rstrip()))) dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def.. 2023. 4. 11.
[백준|파이썬] 4963: 섬의 개수 (실버2) 문제 https://www.acmicpc.net/problem/4963 4963번: 섬의 개수 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 지도의 너비 w와 높이 h가 주어진다. w와 h는 50보다 작거나 같은 양의 정수이다. 둘째 줄부터 h개 줄에는 지도 www.acmicpc.net 🐍파이썬 import sys from collections import deque #상하좌우, 대각선 모두 방문하기 위해 좌표설정 dy = [-1, 1, 0, 0, -1, 1, -1, 1] dx = [0, 0, -1, 1, 1, 1, -1, -1] def bfs(x, y): queue = deque() queue.append((x, y)) while queue: x, y = queue... 2023. 4. 11.
[프로그래머스 | 파이썬 / 자바스크립트] 게임 맵 최단거리(깊이/너비 우선 탐색(DFS/BFS)/level 2) 문제 https://school.programmers.co.kr/learn/courses/30/lessons/1844 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 🐍파이썬 from collections import deque def solution(maps): n = len(maps) #행 m = len(maps[0]) #열 #이동할 좌표, 상하좌우 dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def bfs(x, y): queue = deque() queue.append((x, y)) while queue: x, y = queue.. 2023. 4. 10.
[백준|파이썬] 6186: Best Grass (실버5) 문제 https://www.acmicpc.net/problem/6186 6186번: Best Grass Bessie is planning her day of munching tender spring grass and is gazing out upon the pasture which Farmer John has so lovingly partitioned into a grid with R (1 2023. 4. 10.
[백준|파이썬] 16173: 점프왕 쩰리 (Small) (실버4) 문제 https://www.acmicpc.net/problem/16173 16173번: 점프왕 쩰리 (Small) 쩰리는 맨 왼쪽 위의 칸에서 출발해 (행, 열)로 나타낸 좌표계로, (1, 1) -> (2, 1) -> (3, 1) -> (3, 3)으로 이동해 게임에서 승리할 수 있다. www.acmicpc.net 🐍파이썬 더보기 메모리 초과된 코드 import sys from collections import deque n = int(sys.stdin.readline()) game = [] for _ in range(n): game.append(list(map(int, sys.stdin.readline().split()))) dy = [0, 1] dx = [1, 0] def bfs(x, y): queue.. 2023. 4. 9.
[백준|파이썬] 1012: 유기농 배추 (실버2) 문제 https://www.acmicpc.net/problem/1012 1012번: 유기농 배추 차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 www.acmicpc.net 🐍파이썬 더보기 실패한 코드 import sys from collections import deque t = int(sys.stdin.readline()) for i in range(t): m, n, k = map(int, sys.stdin.readline().split()) baechu = [[0 for _ in range(m)] for _ in range(n)] for _ in range(k): a,.. 2023. 4. 8.
[백준|파이썬] 2644: 촌수계산 (실버2) 문제 https://www.acmicpc.net/problem/2644 2644번: 촌수계산 사람들은 1, 2, 3, …, n (1 ≤ n ≤ 100)의 연속된 번호로 각각 표시된다. 입력 파일의 첫째 줄에는 전체 사람의 수 n이 주어지고, 둘째 줄에는 촌수를 계산해야 하는 서로 다른 두 사람의 번호가 주어 www.acmicpc.net 🐍파이썬 import sys n = int(sys.stdin.readline()) a, b = map(int, sys.stdin.readline().split()) m = int(sys.stdin.readline()) chon = [[] for _ in range(n+1)] for _ in range(1, m+1): c, d = map(int, sys.stdin.readl.. 2023. 4. 7.
반응형