Algorithm 243

[프로그래머스 PCCP 모의고사 1번] 실습용 로봇

def solution(command): answer = [] # 로봇은 입력된 명령에 따라 x, y좌표를 이동 # G : 바라보는 방향으로 한칸 전진 # B : 바라보는 방향으로 한칸 후진 # 위, 오른, 아래, 왼 dx = [0,1,0,-1] dy = [1,0,-1,0] direction = 0 x,y = 0, 0 command = list(command) for com in command: if com == "R": direction = (direction + 1)%4 elif com == "L": if direction > 0: direction -= 1 else: direction = 3 elif com == "G": x += dx[direction] y += dy[direction] elif ..

[백준 7682번] 틱택토

7682번: 틱택토 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 줄은 9개의 문자를 포함하며, 'X', 'O', '.' 중 하나이다. '.'은 빈칸을 의미하며, 9개의 문자는 게임판에서 제일 윗 줄 왼쪽부터의 순서이다. 입 www.acmicpc.net area = [["."]*3 for _ in range(3)] cases = set() def check_is_full(): is_full = True for i in range(3): for j in range(3): if area[i][j] == '.': is_full = False return is_full def check_end(): if check_is_full(): return True if (area[0][0] == area[0][1..

[백준 1182번] 부분수열의 합

1182번: 부분수열의 합 첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다. www.acmicpc.net n, s = map(int,input().split()) # N개의 정수로 이루어진 수열, 부분수열의 원소를 다 더한 값이 S가 되는 경우의 수 data = list(map(int,input().split())) count = 0 visited = [False]*n def dfs(total,index,depth): global count if total == s and depth != 0: count += 1 for i in ..

[백준 2589번] 보물섬

2589번: 보물섬 보물섬 지도를 발견한 후크 선장은 보물을 찾아나섰다. 보물섬 지도는 아래 그림과 같이 직사각형 모양이며 여러 칸으로 나뉘어져 있다. 각 칸은 육지(L)나 바다(W)로 표시되어 있다. 이 지도에서 www.acmicpc.net import sys from collections import deque input = sys.stdin.readline dx = [-1,1,0,0] dy = [0,0,-1,1] # L : 육지, W : 바다 max_value = float("-inf") n, m = map(int,input().split()) area = [list(input().rstrip()) for _ in range(n)] def bfs(x,y): global max_value visite..

Algorithm/BFS 2023.10.20

[백준 14889번] 스타트와 링크

14889번: 스타트와 링크 예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다. www.acmicpc.net n = int(input()) visited = [False for _ in range(n)] data = [list(map(int, input().split())) for _ in range(n)] min_value = float("inf") def dfs(depth,index): global min_value if depth == n//2: power1, power2 = 0, 0 for i in range(n): for j in range(n): if visited[i] and v..

[백준 2798번] 블랙잭

2798번: 블랙잭 첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장 www.acmicpc.net n, m = map(int,input().split()) card_list = list(map(int,input().split())) visited = [False]*n min_diff = float("inf") def dfs(total,count): global min_diff global answer diff = m - total if count == 3: if diff >= 0 and diff < min_diff: min_d..

[백준 7562번] 나이트의 이동

7562번: 나이트의 이동 체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할 수 www.acmicpc.net from collections import deque t = int(input()) dx = [-1,-2,-2,-1,1,2,2,1] dy = [-2,-1,1,2,-2,-1,1,2] for _ in range(t): n = int(input()) start_x, start_y = map(int,input().split()) dest_x, dest_y = map(int,input().split()) visited = [[-1]*n for _ in range(n)] d..

Algorithm/BFS 2023.10.18

[백준 17136번] 색종이 붙이기

17136번: 색종이 붙이기 과 같이 정사각형 모양을 한 다섯 종류의 색종이가 있다. 색종이의 크기는 1×1, 2×2, 3×3, 4×4, 5×5로 총 다섯 종류가 있으며, 각 종류의 색종이는 5개씩 가지고 있다. 색종이를 크 www.acmicpc.net area = [list(map(int,input().split())) for _ in range(10)] size_count = [0]*5 min_value = float("inf") def check(x,y,n): # n 크기의 색종이를 붙일 수 있는 확인하는 함수 if (x + n - 1) >= 10 or (y + n - 1) >= 10: return False for i in range(n+1): for j in range(n+1): if area[..

[백준 1753번] 최단경로 - 다익스트라(Dijkstra Algorithm)

1753번: 최단경로 첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1 ≤ K ≤ V)가 www.acmicpc.net import sys import heapq input = sys.stdin.readline v, e = map(int,input().split()) INF = float("inf") graph = [[] for _ in range(v+1)] distance = [INF]*(v+1) start_node = int(input()) for _ in range(e): start, end, cost = map(int,input().s..