본문 바로가기

Algorithm 💡276

[백준 16724번] 피리 부는 사나이 16724번: 피리 부는 사나이 첫 번째 줄에 지도의 행의 수를 나타내는 N(1 ≤ N ≤ 1,000)과 지도의 열의 수를 나타내는 M(1 ≤ M ≤ 1,000)이 주어진다. 두 번째 줄부터 N개의 줄에 지도의 정보를 나타내는 길이가 M인 문자열이 주 www.acmicpc.net import sys sys.setrecursionlimit(10**5) n,m = map(int,input().split()) area = [list(sys.stdin.readline().rstrip()) for _ in range(n)] visited = [[-1]*m for _ in range(n)] count = 0 index = 0 def dfs(x,y,idx): global count if visited[x][y] !=.. 2023. 8. 20.
[백준 15686번] 치킨 배달 15686번: 치킨 배달 크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸 www.acmicpc.net import itertools def calculate_distance(r1,c1,r2,c2): return abs(r1-r2) + abs(c1-c2) n, m = map(int,input().split()) input_city = [list(map(int,input().split())) for _ in range(n)] chicken_house_list = set() house_list = set() min_value = float("inf.. 2023. 8. 20.
[백준 14890번] 경사로 https://www.acmicpc.net/problem/14890 n, l = map(int,input().split()) area = [list(map(int,input().split())) for _ in range(n)] total = 0 def checkLine(line): inclined = [False for _ in range(n)] for i in range(n-1): if line[i] == line[i+1]: # 높이가 같은 경우에는 Pass continue if abs(line[i]-line[i+1]) >= 2: # 낮은 칸과 높은 칸의 높이 차이가 1이 아닌 경우(조건 2) return False if line[i] > line[i+1]: # 내리막길인 경우 target = lin.. 2023. 8. 17.
[백준 21610번] 마법사 상어와 비바라기 n,m = map(int,input().split()) area = [list(map(int,input().split())) for _ in range(n)] move_list = [] # 이동 정보 목록 for i in range(m): move_list.append(tuple(map(int,input().split()))) cloud_list= {(n-1,0), (n-1,1), (n-2,0), (n-2,1)} # 초기 구름 위치 dx = [0,-1, -1, -1, 0, 1, 1, 1] # 방향 x좌표 dy = [-1, -1, 0, 1, 1, 1, 0, -1] # 방향 y좌표 dx_2 = [-1,-1,1,1] # 대각선 이동을 위한 x좌표 dy_2 = [-1,1,-1,1] # 대각선 이동을 위한 y좌표.. 2023. 8. 15.
[백준 7570번] 줄 세우기 n = int(input()) data = list(map(int,input().split())) data.insert(0,0) index_list = [0]*(n+1) count = 1 max_length = -1 for i in range(1,n+1): index_list[data[i]] = i for i in range(1,n): if index_list[i] < index_list[i+1]: count += 1 max_length = max(max_length, count) else: count = 1 if max_length != -1: print(n - max_length) else: print(0) 이번 문제는 임의의 순서로 세워진 번호를 맨 앞이나 맨 뒤로만 이동시킬 수 있다는 가정 하에 .. 2023. 8. 14.
[백준 2631번] 줄세우기 n = int(input()) data = [] dp = [1]*n for i in range(n): data.append(int(input())) for i in range(n): for j in range(i): if data[i] > data[j]: dp[i] = max(dp[i],dp[j]+1) print(n - max(dp)) 이번 문제는 임의의 순서로 세워진 번호를 번호 순서대로 배치하기 위한 최소한의 이동횟수를 구하는 문제이다. 번호를 옮기는 횟수를 최소화하기 위해서는 번호 순서대로 세워져있는 어린이들을 최대한 많이 고정시키고 나머지만 이동시켜야 한다. 즉, 가장 긴 증가하는 부분수열 LIS(Longest Increasing Subsequence)의 길이를 구하고 이를 전체 어린이 수에서 빼.. 2023. 8. 14.