본문 바로가기

CodingTest/Baekjun Online Judge

(111)
[ BOJ ] 15649 N과 M 백트래킹 관련 문제를 찾다가 풀이한 문제입니다. itertools의 permutation으로 풀면 더 쉬울 것 같은데, 목적이 있어 백트래킹으로 제출했습니다. 코드와 설명 참고. https://veggie-garden.tistory.com/24 [Python] 백트래킹 (+ DFS와 차이) 백트래킹이란? 백트래킹이란 현재 상태에서 가능한 모든 경로를 따라 들어가 탐색하다, 원하는 값과 불일치하는 부분이 발생하면 더 이상 탐색을 진행하지 않고 전 단계로 돌아가는, 즉 이름 그 veggie-garden.tistory.com / 백트래킹 / # 백트래킹 구현 n, m = map(int, input().split()) ans = [] def back(): if len(ans) == m: print(" ".joi..
[ BOJ ] 스타트와 링크 / 제출 1 / from itertools import combinations n = int(input()) s = [list(map(int,input().split())) for _ in range(n)] teamA = list(combinations(range(n), n//2)) teamB = [] for a in teamA: b = [] for i in range(n): if i not in a: b.append(i) teamB.append(tuple(b)) diff = 100 for a, b in zip(teamA, teamB): levA = 0 levB = 0 playerA = list(combinations(a, 2)) playerB = list(combinations(b, 2)) for i,..
[ BOJ ] 구슬 탈출 2 / 구현 1 / n, m = map(int, input().split()) board = [list(input()) for _ in range(n)] for i in range(n): for j in range(m): if board[i][j] == 'R': red = [i, j] elif board[i][j] == 'B': blue = [i, j] elif board[i][j] == 'O': hole = [i, j] movePossible = True count = 0 dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def roll(i, ball): nx = ball[0] + dx[i] ny = ball[1] + dy[i] if board[nx][ny] != '#': return [..
[ 백준 / BOJ ] 2064 IP주소 * 다시 풀기 * 아 .. 얼마 전에 파이썬으로 비트연산하는 거 봐서 가뿐하게 할 수 있을 줄 알았는데.. IP주소 개념 학습이 안되어서 못 푸는 건가.. 하.. / 제출 1 / n = int(input()) ip = [] for i in range(n): ip.append(list(map(int, input().split('.')))) ipCopy = ip.copy() ipAnd = [ipCopy.pop()] while ipCopy: x = ipAnd.pop() y = ipCopy.pop() z = [] for i in range(len(x)): z.append(x[i] & y[i]) ipAnd.append(z) # ipAnd : 주소 address = ipAnd[0] print(*map(str,address), sep..
[ 백준 / BOJ ] 나3곱2 / 제출 1 / n = int(input()) B = list(map(int, input().split())) # 나3, 곱2 => 곱3, 나2 해서 값 나오면 해당 숫자의 이전 값, 둘이 붙여 # 만약 둘 다 안 되면 그게 첫 번째 값 order = [] for num in B: mulThree = num * 3 if num % 2 == 0: divTwo = num // 2 else: divTwo = 0 if divTwo in B: order.append([divTwo, num]) continue elif mulThree in B: order.append([mulThree, num]) continue else: order.append([0, num]) result = [] start = 0 next =..
[ 백준 / BOJ ] 2504 괄호의 값 / 첫 구현 / from collections import deque import sys bracket = list(sys.stdin.readline().strip()) calc = [] so = 0 dae = 0 temp = deque([]) while bracket: now = bracket.popleft() if now == '(': calc.append(2) so += 1 next = bracket.popleft() if next != ')': # 안에 뭐 있으면 calc.append('x') calc.append('(') while so > 0: temp.append(next) if next == elif now == '[': calc.append(3) dae += 1 if bracket[0] ..
[ 백준 / BOJ ] 1966 프린터 큐 / 제출 1 / from collections import deque T = int(input()) for _ in range(T): n, target = map(int, input().split()) docs = enumerate(list(map(int, input().split()))) docs = deque(docs) print(docs) count = 0 while docs: now = docs.popleft() count += 1 if docs: for doc in docs: if now[1] < doc[1]: count -= 1 docs.append(now) break if now[0] == target and now not in docs: break print(count) 풀이 소요시간: 2..
[ 백준 / BOJ ] 16924 십자가 찾기 / 제출 1 / n, m = map(int, input().split()) answer = [] board = [] # 상 하 좌 우 dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] cannot = [] for i in range(n): board.append(list(input().strip())) for x in range(n): for y in range(m): if board[x][y] == '*': size = 1 check = True while check: # check 4 directions for i in range(4): nx = x + dx[i] * size ny = y + dy[i] * size if nx < 0 or n 0: answer.append([x, y,..
[ 백준 / BOJ ] 16922 로마 숫자 만들기 / 제출 1 / from itertools import combinations_with_replacement n = int(input()) count = [] opt = [1, 5, 10, 50] #for i in range(n+1): comb = list(combinations_with_replacement(opt, n)) comb = list(map(list, comb)) for c in comb: if sum(c) not in count: count.append(sum(c)) print(len(count)) 풀이 소요 시간: 22분 중간에 2번 정도 미끄러졌습니다. 1. 중복 조합으로 풀어야 하는데 조합으로 풀었습니다. 조합을 반복해서 카운트를 세면 된다고 생각했습니다. 이건 문제보다는 라이브러리를..
[ 백준 / BOJ ] 16917 양념 반 후라이드 반 / 제출 1 / a, b, c, x, y = map(int, input().split()) sum = 0 if c*2