백준

알고리즘

0615 - 백준 10973⭐

1. 처음 접근 방법 - permutations 함수 from itertools import permutations N = int(input()) input = tuple(map(int, input().split())) list_for_permutation = [i for i in range(1, N+1)] permutation = list(permutations(list_for_permutation, N)) if permutation[0] == input: print(-1) else: for i in range(1, len(permutation)): if permutation[i] == input: print(' '.join(list(map(str, permutation[i-1])))) 처음엔 pyth..

알고리즘

0614 - 백준 10157

1. 백준 10157 C, R = map(int, input().split()) K = int(input()) board = [[0 for i in range(C)] for i in range(R)] if K > R*C: print(0) else: cnt = 1 direction = 0 dx = [-1, 0, 1, 0] # 상우하좌 dy = [0, 1, 0, -1] cr = R-1 cc = 0 while cnt = R or nc >= C or board[nr][nc] != 0: direction = (direction+1) % 4 # ..

알고리즘

0613 - 백준 15686

from collections import deque from itertools import * # INPUT 받아오기 N, M = map(int, input().split()) houses = deque([]) chickens = deque([]) answer = 0 for y in range(N): inp = input().split() for x in range(N): if inp[x] == '1': houses.append((x, y)) if inp[x] == '2': chickens.append((x, y)) # 모든 집에서, 모든 치킨집까지의 거리 미리 계산하기 distances = deque([]) for house in houses: distance = [] #한 집에서 모든 치킨집까지의 ..

알고리즘

0525 - 백준 1013

1. 백준 1013 N = int(input()) results = [] for _ in range(N): input = sys.stdin.readline().rstrip() regex = re.compile('(100+1+|01)+') answer = regex.fullmatch(input) print("YES" if answer else "NO") while문을 써서 난리를 치다가 결국에 re로 작성했다. 2. 파이썬 정규표현식 re 모듈 regex = re.compile('(100+1+|01)+') # print result : re.compile('(100+1+|01)+') compile을 사용해서 정규표현식을 확인할 객체를 만든다. regex.match(input) # 10010111 resul..

알고리즘

0524 - 백준 1931

1. 백준 1931 import sys N = int(input()) meeting = [] answer = 0 for _ in range(N): start, end = map(int, sys.stdin.readline().rstrip().split()) meeting.append((start, end)) meeting.sort(key=lambda x: x[0]) meeting.sort(key=lambda x: x[1]) endTime = 0 for start, end in meeting: if endTime

알고리즘

0523 - 백준 1012

1. 백준 1012 import sys from collections import deque dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def bfs(x, y): q = deque([(x, y)]) board[x][y] = 0 while q: cx, cy = q.popleft() for i in range(4): nx = cx + dx[i] ny = cy + dy[i] if nx = 0 and ny = 0: if board[nx][ny] == 1: board[nx][ny] = 0 q.append((nx, ny)) T = int(input()) for _ in range(T): M, N, K = map(int, sys.stdin.readl..

헬로알파카
'백준' 태그의 글 목록