2024/07 42

[Python][백준] 10655. 마라톤 1/ 구현, 브루트포스 (S3)

링크🔗https://www.acmicpc.net/problem/10655🗒️파이썬 코드 풀이N = int(input())x_lst = []y_lst = []for _ in range(N): x,y = map(int,input().split()) x_lst.append(x) y_lst.append(y)sum = 0 for i in range(0,len(x_lst)-1): sum = sum + abs(x_lst[i]-x_lst[i+1]) + abs(y_lst[i]-y_lst[i+1])mn = 1e100for i in range(1,len(x_lst)-1): prev_mid = abs(x_lst[i-1] - x_lst[i]) + abs(y_lst[i-1] - y_lst[i]) ..

[Python][백준] 2583. 영역 구하기/ 그래프 탐색,DFS,BFS (S1)

링크🔗https://www.acmicpc.net/problem/2583🗒️파이썬 코드 풀이 (DFS)import syssys.setrecursionlimit(10**6)input = sys.stdin.readlineN, M, K = map(int, input().split())lst = [[0] * M for _ in range(N)]for _ in range(K): j1, i1, j2, i2 = map(int, input().split()) for i in range(i1, i2): for j in range(j1, j2): lst[i][j] = 1 di, dj = [0, -1, 0, 1], [1, 0, -1, 0]rs = []def d..

[Python][백준] 11724. 연결 요소의 개수/ 그래프 탐색,DFS,BFS (S2)

링크🔗https://www.acmicpc.net/problem/11724🗒️파이썬 코드 풀이 (DFS)import syssys.setrecursionlimit(10**6)input = sys.stdin.readlinedef dfs(graph,i,visited): visited[i] = True for v in graph[i]: if not (visited[v]): dfs(graph,v,visited)N, M = map(int, input().split())graph = [[] for _ in range(N + 1)]visited = [0] * (N + 1)for _ in range(M): u, v = map(int, input().split()) ..

[Python][백준] 18429. 근손실 / 브루트포스,백트래킹 (S3)

링크🔗https://www.acmicpc.net/problem/18429🗒️파이썬 코드 풀이 (순열)from itertools import permutationsN,K = map(int,input().split())lst = list(map(int,input().split()))comb_lst = list(permutations(lst))rs = 0for c_ls in comb_lst: total = 500 cnt = 0 for c in c_ls: total += (c-K) if total  1. 순열로 푸는 방식은 간단하다. 그냥 모든 조합을 반복문 돌리면 된다. 2. itertools 라이브러리를 호출해서 permutations 함수를 호출 (조합은 c..

[Python][백준] 1935. 후위 표기식2 / 자료,구조스택 (S3)

링크🔗https://www.acmicpc.net/problem/1935🗒️파이썬 코드 풀이N = int(input())lst = list(input())num_lst = [int(input()) for _ in range(N)]stack = []for ls in lst : if "A"  1. 후위 표기식은 스택 구조로 풀이한다. 2. 리스트는 세개를 선언해주었다 . (후위 표기식, 입력값들, 스택) 3. 입력값들로 반복문을 돌리고, 알파벳과 연산자를 구분한다.- 알파벳 같은 경우 stack에 넣어준다- 연산자 같은 경우 stack에서 2개를 pop해주고, 해당 연산자로 계산한다.(뒤 늦게 pop 된 것이 stack1이 되고, 처음으로 pop 된 것은 stack2가 된다.) 4. 문자열 형식 지정..

[Python][백준] 15989. 1, 2, 3 더하기 4 / 구현(S3)

링크🔗https://www.acmicpc.net/problem/16967🗒️파이썬 코드 풀이H,W,X,Y = map(int,input().split(" "))lst_b = [list(map(int,input().split(" "))) for _ in range(H+X)]for h in range(H): for w in range(W): lst_b[h+X][w+Y] = lst_b[h+X][w+Y] - lst_b[h][w] print(lst_b[h][w], end=" ") print() 1. 단순 구현 문제이다.  2. 배열  A의 크기는 H,W이고, A에서 (X,Y) 만큼 이동 한 것을 합한게 B 이므로,합쳐지는 과정에 겹치는 것을 빼주면 된다. ex) H =2, W..

[Python][백준] 15989. 1, 2, 3 더하기 4 / DP

링크🔗https://www.acmicpc.net/problem/15989🗒️파이썬 코드 풀이N = int(input())dp = [1] * 11000print(dp[0:11])for i in range(2,11000): dp[i] = dp[i] + dp[i-2]for i in range(3,11000): dp[i] = dp[i] + dp[i-3]for _ in range(N): print(dp[int(input())]) 1. DP로 푸는 문제이다.  2. 0~11000까지 넉넉히 DP 리스트를 만들어 준다. 3. 입력값을 쪼개었을 때, 2가 나오는 수를 누적시켜준다. 4. 2를 누적시킨 리스트에, 3이 나오는 수를 누적시준다. 5. DP 리스트가 만들어졌으므로, 입력 값을 dp 인덱스..

[Python][백준] 1446. 지름길

링크🔗https://www.acmicpc.net/problem/1446🗒️파이썬 코드 풀이N,D = map(int,input().split(' '))lst = [list(map(int,input().split(' '))) for _ in range(N)]lst = sorted(lst)dp = [i for i in range(D+1)]k = 0for i in range(len(dp)): dp[i] = min(dp[i-1]+1,dp[i]) while k 1. DP로 푸는 문제이다.  2. 0~D(고속도로 길이)까지 dp 리스트를 만들어준다. 3. 0~D까지의 반복문을 돌리면서 현재 dp[i] 와, 이전 dp[i-1] + 1 의 dp 값 중 최소 값을 dp[i]로 한다.(이렇게 하는 이유는 지름..

[Python][백준] 1927. 문자열 교환

링크🔗https://www.acmicpc.net/problem/1522🗒️파이썬 코드 풀이lst = list(input())a_cnt = lst.count("a")circle_lst = lst + lstmn = 10000for i in range(len(lst)): tmp_lst = circle_lst[i : i + a_cnt] mn = min(mn, tmp_lst.count("b"))print(mn) 1. 브루트포스로 하나 하나 다 확인을 하는데, 슬라이딩 윈도우 방식을 쓰면 편하다.  2. 문제의 문자열은 원형의 특성을 가지고 있기 때문에, lst + lst로 하여 연결시켰다.(브루트포스로 확인 할 때는 기존 lst에 있는 것만 확인하면 된다.) 3. 교환을 하더라도 a의 개수는 정해저..