본문 바로가기

반응형

알고리즘/백준 ~ 단계별 풀어보기

(21)
[다시 풀어보기] 백준 14888번 "연산자 끼워넣기" 파이썬 import sys n = int(sys.stdin.readline().rstrip()) nums = list(map(int, sys.stdin.readline().split())) a, s, m, d = map(int, sys.stdin.readline().split()) minmaxList= [] def dfs(i, num): global a, s, m, d, minmaxList if i == n: minmaxList.append(num) return if a > 0 : a -= 1 dfs(i+1, num +nums[i]) a += 1 if s > 0 : s -= 1 dfs(i+1, num - nums[i]) s += 1 if m > 0 : m -= 1 dfs(i+1, num * nums[i]..
[다시 풀어보기] 백준 9663번 "N-Queen" 파이썬 import sys n = int(sys.stdin.readline()) ans = 0 row = [0] * n def is_promising(x): for i in range(x): if (row[x] == row[i]) or (abs(row[x] - row[i]) == abs(x-i) ): return False return True def n_queens(x): global ans if x == n : ans += 1 return else : for i in range(n): row[x] = i if is_promising(x): n_queens(x+1) n_queens(0) print( ans) # 참고 : https://seongonion.tistory.com/103 자세한 풀이 🔜 ht..
백준 15652번 "N과 M (4)" 파이썬 import itertools import sys n , m = map(int, sys.stdin.readline().split()) ans_list = [] def dfs(): if len(ans_list) == m: print(" ".join(map(str, ans_list))) return for i in range(1, n+1): if not ans_list: ans_list.append(i) dfs() ans_list.pop() elif i >= ans_list[-1]: ans_list.append(i) dfs() ans_list.pop() dfs()
백준 15651번 "N과 M(3)" 파이썬 import itertools import sys n , m = map(int, sys.stdin.readline().split()) ans_list = [] def dfs(): if len(ans_list) == m: print(" ".join(map(str, ans_list))) return for i in range(1, n+1): ans_list.append(i) dfs() ans_list.pop() dfs()
백준 15650 "N과 M (2)" 파이썬 import itertools import sys n , m = map(int, sys.stdin.readline().split()) ans_list = [] def dfs(): if len(ans_list) == m: print(" ".join(map(str, ans_list))) for i in range(1, n+1): if not ans_list: ans_list.append(i) dfs() ans_list.pop() elif i not in ans_list and i > ans_list[-1]: ans_list.append(i) dfs() ans_list.pop() dfs()
백준 15649번 "N과 M" 파이썬 import itertools import sys n , m = map(int, sys.stdin.readline().split()) ans = itertools.permutations(range(1, n+1), m) for i in ans: print(' '.join(map(str, i))) import itertools import sys n , m = map(int, sys.stdin.readline().split()) ans_list = [] def dfs(): if len(ans_list) == m: print(" ".join(map(str, ans_list))) for i in range(1, n+1): if i not in ans_list: ans_list.append(i) dfs()..
백준 3009 "네 번째 점" 파이썬 from collections import Counter from itertools import count import sys x1, y1 = map(int , sys.stdin.readline().split()) x2, y2 = map(int , sys.stdin.readline().split()) x3, y3 = map(int , sys.stdin.readline().split()) list_x = list() list_x.append(x1) list_x.append(x2) list_x.append(x3) list_y = list() list_y.append(y1) list_y.append(y2) list_y.append(y3) x = 0 y = 0 for i in list_x: if Coun..
백준 1085번 "직사각형에서 탈출" Python import sys x, y, w, h = map(int , sys.stdin.readline().split()) tempList = list() if x < w - x: tempList.append(x) else: tempList.append(w - x) if y < h - y : tempList.append(y) else: tempList.append(h-y) print(min(tempList))