목록분류 전체보기 (310)
가오리의 코딩일기
n = int(input()) treeTable = [list(map(int, input())) for _ in range(n)] def dfs(x, y, n): checkPoint = treeTable[x][y] for i in range(x, x+n): for j in range(y, y+n): if checkPoint != treeTable[i][j]: checkPoint = -1 break if checkPoint == -1: print('(', end='') n //= 2 dfs(x, y, n) dfs(x, y+n, n) dfs(x+n, y, n) dfs(x+n, y+n, n) print(')', end='') elif checkPoint == 1: print(1, end='') else: pr..
import sys sys.setrecursionlimit(10**6) def dfs(x,y): # 상하좌우+대각선 dx = [1,1,-1,-1,1,-1,0,0] dy = [0,1,0,1,-1,-1,1,-1] table[x][y]= 0 for i in range(8): nx = x+dx[i] ny = y+dy[i] if (0
n = int(input()) def hanoi(n, a, b, c): if n==1: print(a, c) else: hanoi(n-1, a, c, b) print(a, c) hanoi(n-1, b, a, c) result = 1 for i in range(n-1): result = result *2+1 print(result) hanoi(n,1,2,3)
import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) n = int(input()) visited = [False] * (n+1) graph = [[] for _ in range(n+1)] for i in range(n-1): num1, num2 = map(int, input().split()) graph[num1].append(num2) graph[num2].append(num1) def dfs(x): for i in graph[x]: if visited[i] == 0: visited[i] = x dfs(i) dfs(1) for i in range(2, n+1): print(visited[i])
n = int(input()) paper = [list(map(int, input().split())) for _ in range(n)] minus = 0 zero = 0 plus = 0 def dividePaper(x, y, n): global minus, zero, plus checkNumber = paper[x][y] for i in range(x, x+n): for j in range(y, y+n): if paper[i][j] != checkNumber: for k in range(3): for l in range(3): dividePaper(x+k*n//3, y+l*n//3, n//3) return None if checkNumber == -1: minus += 1 elif checkNumber..
bisect: 파이썬 내장 이진탐색 모듈 bisect_left(literable, value): 왼쪽 인덱스 구하기 bisect_right(literable, value): 오른쪽 인덱스 구하기 from bisect import bisect_left, bisect_right n = int(input()) card = sorted(map(int, input().split())) m = int(input()) compare = list(map(int, input().split())) def count_by_range(array, leftValue, rightValue): rightIndex = bisect_right(array, rightValue) leftIndex = bisect_left(array, l..
n = int(input()) tree = [[0] * 3 for _ in range(26)] def preorder(x): # root -> left -> right print(x, end='') if tree[ord(x)-65][1] != '.': preorder(tree[ord(x)-65][1]) if tree[ord(x)-65][2] != '.': preorder(tree[ord(x)-65][2]) def inorder(x): # left -> root -> right if tree[ord(x)-65][1] != '.': inorder(tree[ord(x)-65][1]) print(x, end='') if tree[ord(x)-65][2] != '.': inorder(tree[ord(x)-65][..
# [2331] 반복수열 a, p = map(int, input().split()) numberList = [a] while True: newNumber = 0 for i in str(numberList[-1]): newNumber += int(i) ** p if newNumber in numberList: break numberList.append(newNumber) print(numberList.index(newNumber))
from collections import deque n, m = map(int, input().split()) graph = [] for _ in range(n): graph.append(list(map(int, input()))) dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def bfs(x, y): queue = deque() queue.append((x, y)) while queue: x, y = queue.popleft() for i in range(4): nx = x+dx[i] ny = y+dy[i] if nx = n or ny = m: continue if graph[nx][ny] == 0: continue if graph[nx]..
n = int(input()) graph = [] house = [] for _ in range(n): graph.append(list(map(int, input()))) dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def dfs(x, y): if x = n or y = n: return False if graph[x][y] == 1: global count count += 1 graph[x][y] = 0 for i in range(4): nx = x + dx[i] ny = y + dy[i] dfs(nx, ny) return True return False count = 0 result = 0 for i in range(n): for j in r..