practivceAlgorithm/백준 379

[백준][Python] 17825 주사위 윷놀이

움직이는 말의 조합 2^20승에 대해 각각 10번의 이동을 조사하며 point연산. 중요로직은 section을 나누어 이동시키는 것.(shortcut) 이미 말이 있는경우 불가능처리시키는 것. import sys input = sys.stdin.readline def calculate(): global result # 각 플레이어의 구간, 위치정보 players = [[0, 0] for _ in range(5)] # 각 경우의 수에서 포인트의 합 sum_points = 0 # 1턴 부터 10턴까지 실행. for i in range(1, 11): now = turns[i] section, pos = players[now] if section == -1: return else: pos += dice[i] #..

[BOJ][Python] 17822 원판돌리기

오랜만에 구현문제를 풀어보았습니다. 회전은 k를 M주기로 나눈 나머지만큼 한번에 돌렸고 회전이 끝난 후엔 각 지점을 bfs로 추적하며 같은 부분을 제거시켰으며 제거가 없는 시점에서는 각 부분을 조사해 평균치보다 크거나 작으면 보정치를 증감해주었습니다. import sys input = sys.stdin.readline from collections import deque def rotate(i, d): new_board = [] if d: for n in range(k, M): new_board.append(round_boards[i][n]) for n in range(k): new_board.append(round_boards[i][n]) else: for n in range(M - k, M): new..

[백준][Python] 17837 새로운게임2

위에서 옮기는 것의 순서만 잘 조절해주면 나머지는 색에 따라 구분만 해주면 된다. import sys input = sys.stdin.readline def move_white(x, y, nx, ny): now = chess[x][y].index(coin) top = len(chess[x][y]) for i in range(now, top): coins[chess[x][y][i]][0] = nx coins[chess[x][y][i]][1] = ny chess[nx][ny].append(chess[x][y][i]) for _ in range(top - now): chess[x][y].pop() def move_red(x, y, nx, ny): now = chess[x][y].index(coin) top =..

[백준][Python] 21610 마법사상어와 비바라기

움직인 구름의 경우 추후 제외하고 구름을 생성하기 위해 set으로 관리. 움직이기 전 구름은 list로 뺐다 넣었다 하면서 관리. 이동은 N의 나머지로 관리 import sys input = sys.stdin.readline N, M = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(N)] moves = [tuple(map(int, input().split())) for _ in range(M)] delta = ((0, 0), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)) check = ((-1, 1), (1, 1), (-1, -1),..

[백준][Python] 17779 게리멘더링2

단순히 영역을 나누는 문제였습니다. 대각선에 대해 4번 범위 설정이 필요한 문제였습니다. 핵심은 1개씩 늘어나거나 줄어드는 dx, dy설정을 어떻게 할 것인가? 결국 i에서 i의 초깃값을 빼주면 0부터 시작한 다는 것을 알 수 있었습니다. import sys input = sys.stdin.readline N = int(input()) matrix = [list(map(int, input().split())) for _ in range(N)] answer = float('inf') total = 0 for i in range(N): for j in range(N): total += matrix[i][j] for x in range(N - 1): for y in range(1, N - 1): for d1 ..

[백준][Python] 17143 낚시왕

처음에 상어끼리 잡아먹는다는 문장을 못봐서 헤멤. 본 후에는 heap기반 -> 2차원 배열 직접 생성으로 바꿔서 품. import sys input = sys.stdin.readline def move_shark(): new_board = [[0 for _ in range(C)] for _ in range(R)] for x in range(R): for y in range(C): if not board[x][y]: continue t_x, t_y = x, y s, d, z = board[t_x][t_y] total = s if d == 0 or d == 1: s %= 2 * R - 2 while s: if (t_x == R - 1 and d == 1) or (t_x == 0 and d == 0): d ^..

[백준][Python] 11444 피보나치수6 - 행렬의 곱셈

행렬의 곱셈을 연습하는 문제이다. 각 항을 어떻게 표현 할 것인가? 2x2 행렬의 i행 j열의 요소는 두개의 요소의 곱이 두개씩 합해져 들어간다. def matrix_mult(a, b): ret = [[0 ,0], [0, 0]] for i in range(2): for j in range(2): for k in range(2): ret[i][j] += (a[i][k] * b[k][j]) % MOD return ret n = bin(int(input()))[2:] MOD = 1000000007 base = [[1, 1], [1, 0]] answer = [[1, 0], [0, 1]] for i in range(1, len(n) + 1): if n[-i] == '1': answer = matrix_mult(a..

[백준][Python] 11404 플로이드

플로이드 워셜 기본문제 각 기점에 대해 모든 경로를 전수조사한다. 한번당 기점 하나를 거치는 경로이다. import sys input = sys.stdin.readline n, m = int(input()), int(input()) bus_datas = [[float('inf')] * n for _ in range(n)] for _ in range(m): a, b, c = map(int, input().split()) bus_datas[a - 1][b - 1] = min(bus_datas[a - 1][b - 1], c) for k in range(n): bus_datas[k][k] = 0 for i in range(n): for j in range(n): if bus_datas[i][j] > bus_da..

[백준][Python] 2263 트리의 순회

분할 정복 in_order로 root기준 오른쪽 왼쪽을 나누고 post_order로 root를 찾는다. import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) def find_tree(in_left, in_right, post_left, post_right): if in_left > in_right or post_left > post_right: return root = post_order[post_right] root_idx = in_order_idx[root] pre_order.append(root) left_size = root_idx - in_left find_tree(in_left, root_idx - 1, post_left, po..