본문 바로가기

728x90

1. Programming

(17)
[Baekjoon/Python] 16928번: 뱀과 사다리 게임(BFS) # Baekjoon 16928번: 뱀과 사다리 게임 import sys from collections import deque input = sys.stdin.readline N, M = map(int, input().split()) visited = [0 for _ in range(101)] ladder = [tuple(map(int, input().split())) for _ in range(N)] snake = [tuple(map(int, input().split())) for _ in range(M)] visited_ladder = [None for _ in range(101)] visited_snake = [None for _ in range(101)] for x, y in ladder: visi..
[Baekjoon/Python] 1759번: 암호 만들기(BackTracking) # Baekjoon 1759번: 암호 만들기 import sys input = sys.stdin.readline L, C = map(int, input().split()) code = input().split() code.sort() def DFS(cstr, m): global L if m == C: if len(cstr) == L: count = 0 flag = False for char in cstr: if char in 'aeiou': flag = True else: count += 1 if flag == True and 1 < count: print(cstr) return DFS(cstr + code[m], m+1) DFS(cstr, m+1) DFS("", 0) 이번 문제는 백트래킹 문제이다. 문..
[Baekjoon/Python] 15686번: 치킨 배달(BackTracking) # Baekjoon 15686번: 치킨 배달 import sys from collections import deque from itertools import combinations input = sys.stdin.readline N, M = map(int, input().split()) city = [[] for _ in range(N)] market_info = [] house_info = [] tlst = [] for i in range(N): arr = input().split() for j in range(N): city[i].append(int(arr[j])) if arr[j] == '2': market_info.append((i, j)) elif arr[j] == '1': house_info...
[Baekjoon/Python] 14940번: 쉬운 최단거리(BFS) # Baekjoon 14940번: 쉬운 최단거리 import sys from collections import deque input = sys.stdin.readline n, m = map(int, input().split()) arr = [] sol_arr = [[None] * m for _ in range(n)] des_x, des_y = -1, -1 for i in range(n): distance = list(map(int, input().split())) arr.append(distance) for j in range(m): if distance[j] == 2: des_x = j des_y = i elif distance[j] == 0: sol_arr[i][j] = 0 def BFS(): sol..
[Baekjoon/Python] 11724번: 연결 요소의 개수(BFS) # Baekjoon 11724번: 연결 요소의 개수 import sys from collections import deque input = sys.stdin.readline N, M = map(int, input().split()) visited = [None for _ in range(N+1)] graph = [[] for _ in range(N+1)] cnt = 0 for i in range(M): u, v = map(int, input().split()) graph[u].append(v) graph[v].append(u) def BFS(i): global cnt q = deque() q.append(graph[i]) visited[i] = True while q: for v in q.popleft(..
[Baekjoon/Python] 1931번: 회의실 배정(Greedy) # Baekjoon 1931번: 회의실 배정 import sys input = sys.stdin.readline N = int(input()) room = [] for i in range(N): start, end = map(int, input().split()) room.append((start, end)) room = sorted(room, key = lambda x: x[0]) room = sorted(room, key = lambda x: x[1]) room_max = 0 present_end = 0 for s, e in room: if present_end 6, 10 으로 정렬이 되면 6, 10을 카운트하지 못하기에 고려해야 한다.
[Baekjoon/Python] 2178번: 미로 탐색(BFS) # Baekjoon 2178번: 미로 탐색 import sys from collections import deque input = sys.stdin.readline N, K = map(int, input().split()) maze = [[] for _ in range(N)] visited = [[] for _ in range(N+1)] for i in range(N): for j in range(K): visited[i].append(0) for i in range(N): maze_num = input() for j in maze_num: maze[i].append(j) def BFS(): visited[0][0] = 1 queue = deque() queue.append((0, 0)) while qu..
[Baekjoon/Python] 1697번: 숨바꼭질(BFS) # Baekjoon 1697번: 숨바꼭질 import sys from collections import deque input = sys.stdin.readline N, K = map(int, input().split()) visited = [0] * 2000002 def BFS(pos): queue = deque() queue.append(pos) visited[N] = 1 while queue: c = queue.popleft() if c == K: return visited[c]-1 for i in (c-1, c+1, 2*c): if visited[i] == 0 and 0

728x90