BFS 36

[백준] 2583 영역구하기

www.acmicpc.net/problem/2583 2583번: 영역 구하기 첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오 www.acmicpc.net from collections import deque def bfs(row, col): q = deque() dr = [-1, 1, 0, 0] dc = [0, 0, -1, 1] q.append((row, col)) matrix[row][col] = 1 while q: nr, nc = q.popleft() for i in range(4): if 0

STUDY/Algorithm 2021.03.04

[백준] 7569 토마토

www.acmicpc.net/problem/7569 7569번: 토마토 첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100, www.acmicpc.net 이번엔 3차원배열이다. 2차원 배열에서 z축이 하나 더 생겼지만, 6개만 확인하면 되는 문제이므로 난이도가 확 올라가진 않는다. 여러번 시도했으나 지난 토마토 문제와 같이 시간초과가 떴다. 그래서 pypy로 확인해 보니 정답. 알고리즘엔 문제가 없는걸 확인했다. 다음은 pypy에서 돌아간 코드이다 from collections import deque dz = [0, 0, 0, 0, -1,..

STUDY/Algorithm 2021.03.02

[백준] 7576 토마토

https://acmicpc.net/problem/7576 7576번: 토마토 첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토 www.acmicpc.net from sys import stdin from collections import deque input = stdin.readline m, n = map(int, input().split()) q = deque() tomato = [] for r in range(n): tmp = list(map(int, input().split())) tomato.append(tmp) for c in r..

STUDY/Algorithm 2021.03.02

[백준] 10026 적록색약

www.acmicpc.net/problem/10026 10026번: 적록색약 적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록) www.acmicpc.net def dfs1(row, col, matrix, visited): delta = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 상하좌우 stack = [(row, col)] while stack: visit_node = stack[-1] visited[visit_node[0]][visit_node[1]] = 1 adjacent = [] for dt in delta: if 0

STUDY/Algorithm 2021.03.01

[프로그래머스] LEVEL3 단어 변환, python3, 깊이/너비 우선 탐색(DFS/BFS)

def solution(begin, target, words): if not target in words: return 0 answer = 0 queue = [begin] while words!=[]: answer+=1 tmp=[] while queue: word_stack=queue.pop(0) for word in words: change=0 for i in range(len(word)): if word[i]==word_stack[i]: change+=1 if change==len(word)-1: tmp.append(word) words= list([word for word in words if word not in tmp]) #print("after:",tmp,words) if tmp==[] and..

STUDY/Algorithm 2020.01.17

[프로그래머스] LEVEL2 타겟 넘버, python3, 깊이/너비 우선 탐색(DFS/BFS)

def solution(numbers, target): answer_list=[0] for i in numbers: temporary_list=[] #print("\nanswer_list:",answer_list) for j in answer_list: temporary_list.append(j+i) temporary_list.append(j-i) #print("tmp_list",temporary_list) answer_list=temporary_list #print("answer_list",answer_list) answer = answer_list.count(target) return answer 방법은 똑같았으나 구현하지 못해 내 코드는 아니지만 올린다.

STUDY/Algorithm 2019.11.20