프로그래밍/Algorithm 105

백준 20191 줄임말 파이썬

www.acmicpc.net/problem/20191 20191번: 줄임말 문자열 A가 문자열 B의 줄임말이라는 것은 B의 순서를 바꾸지 않고 0 또는 그 이상 개수의 문자를 지워 A를 만들 수 있다는 뜻이다. 정의에 의해서 B는 자기 자신의 줄임말임에 유의하라. 예를 들 www.acmicpc.net from bisect import bisect_right, bisect_left if __name__ == '__main__': s = input() t = input() # s = 'caa' # t = 'ac' next = [[] for _ in range(26)] for i in range(len(t)): next[ord(t[i]) - ord('a')].append(i) def solution(ss): ..

백준 16234 인구 이동 파이썬

www.acmicpc.net/problem/16234 16234번: 인구 이동 N×N크기의 땅이 있고, 땅은 1×1개의 칸으로 나누어져 있다. 각각의 땅에는 나라가 하나씩 존재하며, r행 c열에 있는 나라에는 A[r][c]명이 살고 있다. 인접한 나라 사이에는 국경선이 존재한다. 모 www.acmicpc.net 와.. 이 문제 역대급이였다. 결국 혼자의 힘으로 풀어내긴 했지만 이틀 정도 헤맨것같다. 풀고나서 다른분들 풀이를 보니 내가 왜 이생각을 못했나.. 자괴감들고 괴로워.. ㅠ import sys from collections import deque if __name__ == '__main__': direction = [(0, 1), (1, 0), (0, -1), (-1, 0)] n, left, ri..

백준 1389 케빈 베이컨의 6단계 법칙

www.acmicpc.net/problem/1389 1389번: 케빈 베이컨의 6단계 법칙 첫째 줄에 유저의 수 N (2 ≤ N ≤ 100)과 친구 관계의 수 M (1 ≤ M ≤ 5,000)이 주어진다. 둘째 줄부터 M개의 줄에는 친구 관계가 주어진다. 친구 관계는 A와 B로 이루어져 있으며, A와 B가 친구라는 뜻 www.acmicpc.net import sys from collections import deque if __name__ == '__main__': n, m = map(int, sys.stdin.readline().rstrip().split()) graph = [[] for _ in range(n + 1)] for _ in range(m): a, b = map(int, sys.stdin.r..

백준 11725 트리의 부모 찾기 파이썬

www.acmicpc.net/problem/11725 11725번: 트리의 부모 찾기 루트 없는 트리가 주어진다. 이때, 트리의 루트를 1이라고 정했을 때, 각 노드의 부모를 구하는 프로그램을 작성하시오. www.acmicpc.net import sys from collections import deque sys.setrecursionlimit(100001) if __name__ == '__main__': n = int(sys.stdin.readline().rstrip()) graph = [[] for _ in range(n + 1)] for i in range(n - 1): a, b = map(int, sys.stdin.readline().split()) graph[a].append(b) graph[b..

백준 3055 탈출 파이썬

www.acmicpc.net/problem/3055 3055번: 탈출 사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제 www.acmicpc.net import sys from collections import deque if __name__ == '__main__': n, m = map(int, sys.stdin.readline().rstrip().split()) t_ddup_forest = [] hedgehog_pos = [] water_pos = deque() current_pos = [[False] * m for _ in range(n)] for i in ran..

백준 아기 상어 파이썬

www.acmicpc.net/problem/16236 16236번: 아기 상어 N×N 크기의 공간에 물고기 M마리와 아기 상어 1마리가 있다. 공간은 1×1 크기의 정사각형 칸으로 나누어져 있다. 한 칸에는 물고기가 최대 1마리 존재한다. 아기 상어와 물고기는 모두 크기를 가 www.acmicpc.net import sys from collections import deque sys.setrecursionlimit(10000) if __name__ == '__main__': n = int(sys.stdin.readline().rstrip()) sea = [] eatable = False baby_shark = 9 shark_size = 2 shark_pos = [] for i in range(n): nu..

백준 구슬탈출2 파이썬

www.acmicpc.net/problem/13460 13460번: 구슬 탈출 2 첫 번째 줄에는 보드의 세로, 가로 크기를 의미하는 두 정수 N, M (3 ≤ N, M ≤ 10)이 주어진다. 다음 N개의 줄에 보드의 모양을 나타내는 길이 M의 문자열이 주어진다. 이 문자열은 '.', '#', 'O', 'R', 'B' www.acmicpc.net import sys from collections import deque sys.setrecursionlimit(10000) if __name__ == '__main__': N, M = map(int, sys.stdin.readline().rstrip().split()) ball_start_pos = {} graph = [] for i in range(N): d..

백준 2206 벽 부수고 이동하기 파이썬

www.acmicpc.net/problem/2206 2206번: 벽 부수고 이동하기 N×M의 행렬로 표현되는 맵이 있다. 맵에서 0은 이동할 수 있는 곳을 나타내고, 1은 이동할 수 없는 벽이 있는 곳을 나타낸다. 당신은 (1, 1)에서 (N, M)의 위치까지 이동하려 하는데, 이때 최단 경로 www.acmicpc.net import sys from collections import deque sys.setrecursionlimit(10000) if __name__ == '__main__': N, M = map(int, sys.stdin.readline().rstrip('\n').split()) graph = [] wall_points = [] for i in range(N): l = list(sys.s..

백준 7569 토마토 파이썬

www.acmicpc.net/problem/7569 7569번: 토마토 첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100, www.acmicpc.net import sys from collections import deque sys.setrecursionlimit(10003) if __name__ == '__main__': M, N, H = map(int, sys.stdin.readline().rstrip().split()) graph = [] well_tomato_q = deque() all_well_flag = True for i i..

백준 2468 안전 영역 파이썬

www.acmicpc.net/problem/2468 2468번: 안전 영역 재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는 www.acmicpc.net import sys import copy sys.setrecursionlimit(10003) if __name__ == '__main__': N = int(sys.stdin.readline().rstrip()) graph = [] rains = set() for i in range(N): point = list(map(int, sys.stdin.readline().rstrip().split())) graph.appen..