알고리즘 14

[Algorithms] 백준 2667번: 단지번호붙이기

BFS로 문제를 해결했다. 앞서 DFS를 풀면서, BFS도 같이 공부한것이 도움이 되었다. from collections import deque dx = [0, 0, 1, -1] dy = [1, -1, 0, 0] def bfs(graph, a, b): n = len(graph) queue = deque() queue.append((a, b)) graph[a][b] = 0 count = 1 while queue: x, y = queue.popleft() for i in range(4): nx = x + dx[i] ny = y + dy[i] if nx = n or ny = n: continue if graph[nx][ny] == 1: graph[nx][ny] = 0 q..

알고리즘 2023.08.21

[Algorithms] 백준 1966번: 프린터

초기에 주어진 문서리스트를 (문서의 리스트인덱스, 문서 중요도)의 튜플로 만들고 문제를 해결했다. import sys input=sys.stdin.readline T=int(input()) for _ in range(T): num_doc,doc_to_know=map(int,input().split(" ")) imp_list=list(map(int,input().split(" "))) imp_seq_list=[] for i in range(num_doc): imp_seq_list.append((i,imp_list[i])) doc_impt=imp_seq_list[doc_to_know] print_doc='' cnt=0 while print_doc!=doc_impt: if imp_seq_list[0][1] <..

알고리즘 2023.08.14

[Algorithms] 백준 1406번: 에디터

백준 1406번은 간단한 에디터를 구현하는 문제였다. 처음에는 배열과 스택 하나만으로 해결하려고 했는데, 잘 안되어서 구글링해서 해결했다. 두개의 스택을 이용해서 커서와 같이 구현을 해놓은 코드를 이용했다. import sys stack_l = list(input()) stack_r = [] n = int(input()) for i in range(n): command = sys.stdin.readline().split() if command[0] == "L" and stack_l: stack_r.append(stack_l.pop()) elif command[0] == "D" and stack_r: stack_l.append(stack_r.pop()) elif command[0] == "B" and st..

알고리즘 2023.08.13

[Algorithms] 백준 1012번: 유기농 배추

이번에 현대엔지비에서 하는 softeer 준비를 하느라 백준에서 여러문제를 풀어보기 시작했다. 백준 1012번 같은 경우는 BFS로 문제를 풀었다. import sys input=sys.stdin.readline dx=[0,0,1,-1] dy=[1,-1,0,0] def BFS(graph,i,j): # print("BFS CALLED") queue=[] queue.append((i,j)) graph[i][j]=0 count=1 while queue: x,y=queue.pop(0) for j in range(4): nx=x+dx[j] ny=y+dy[j] if nx=len(graph) or ny=len(graph[0]): continue if graph[nx][ny]==1: count=count+1 graph..

알고리즘 2023.08.13