본문 바로가기

Language/python4

[파이썬] 백준 뱀 문제 3190 https://www.acmicpc.net/problem/3190from collections import dequea,b = 0,0d= 0mi = 0n = int(input())k = int(input())group = set()for _ in range(k): x,y = map(int,input().split()) group.add((x-1,y-1)) L = int(input())X = []C = []for i in range(L): x,c = input().split() X.append(int(x)) C.append(c) turns = dict(zip(X, C)) dx = [0,1,0,-1]dy = [1,0,-1,0]def dir(d): dl = (d.. 2025. 4. 20.
[파이썬] 백준 로봇청소기 문제 코드 정리. https://www.acmicpc.net/problem/14503from collections import dequen,m = map(int,input().split())r,c,d=map(int,input().split())graph = []for _ in range(n): graph.append(list(map(int,input().split())))#북 동 남 서 cx = [-1, 0, 1, 0] cy= [0, 1, 0, -1]def turn_left(d): #방향 계산하는 일반 함수. return (d + 3) % 4 #하나의 청소기가 움직이는 것이므로 deque는 필요없음def move(r, c, d): count = 0 #청소한 갯수를 세어야하므로. while True: if g.. 2025. 4. 7.
[파이썬]list(input()), list(map(int,input().split())) 차이 list(input()) : 문자열 한 글자씩 분해해서 저장해버리게 된다.예를 들어, 입력으로 1 0 0 이라고 썼다고 해본다.input() → "1 0 0"list(input()) → ['1', ' ', '0', ' ', '0'] ← 공백도 문자로 들어가게 된다.map(int, input().split()) → [1, 0, 0] ← 숫자만 리스트에 들어가게 된다. ex) graph = []graph.append(list(map(int,input().split()))) 2025. 4. 7.
deque와 list 차이 deque(double-ended queue)는 양쪽 끝에 원소를 추가하거나 삭제할 수 있음.list는 마지막 원소를 추가 삭제하는 것을 기본으로 함. deque(double-ended queue)append, appendleft, pop, popleft를 사용하여 양쪽 끝에서 추가/삭제하므로 시간복잡도 : O(1) list마지막 끝에서 추가/삭제함을 기본으로 할 때 : O(1)앞쪽에서 추가/삭제하거나 중간에서 추가/삭제 할 때 동적 배열을 원칙으로 하므로 시간 복잡도: O(n) 양쪽에서 삽입/삭제, 큐(FIFO), 데큐(FIFO), 스택 구현에는 deque가 유리함. 큐, 데큐처럼 사용하고 싶으면 popleft, appendleft로 FIFO 구현 가능.스택처럼 사용할 경우 pop, append 사용하.. 2025. 3. 24.