본문 바로가기
반응형

SWE326

HackerRank - Castle on the Grid 문제 : https://www.hackerrank.com/challenges/castle-on-the-grid/problem 코드 : BFS의 응용 버전 두번째 코드 (성공!) from collections import deque def minimumMoves(grid, startX, startY, goalX, goalY): n = len(grid) m = len(grid[0]) maximum = n*m # n*m graph graph = [[maximum]*m for _ in range(n)] graph[startX][startY] = 0 # for debugging def printGraph(): print('====printGraph=====') for i in range(n): for j in r.. 2022. 3. 5.
python - BFS - problem solving example 강의: https://youtu.be/7C9RgOcvkvo 코드: # BFS example from collections import deque # 1. input n, m = map(int, input.split()) graph=[] for i in range(n): graph[i].append(list(map(int, input()))) # 2. find shortest path # top, bottom, left, right dr = [-1, 1, 0, 0] dc = [0, 0, -1, 1] def bfs(r,c): queue = deque() queue.append(r,c) while queue: r,c = queue.popleft() for d in range(4): nr = r + dr[d] .. 2022. 3. 5.
Python - DFS Problem solving example 강의 : https://youtu.be/7C9RgOcvkvo 코드 : # example - DFS problem # 1. input n, m = map(int, input().split()) graph = [] for i in range(n): graph.append(list(map(int, input()))) # *no space* def dfs(r,c): # check out of boundary if rn or c=m: return False: # check visited if graph[r][c]==0: graph[r][c] =1 # top, left, bottom, right dfs(r-1, c) dfs(r, c-1) dfs(r+1, c) dfs(r, c+1) return True return .. 2022. 3. 5.
Python 사용 모음 recursion limit import sys sys.setrecursionlimit(2000) 사용자 입력받기 # 사용자 입력받기 i = input() #default: str # Integer로 변환하기 num = int(input()) # integer list 입력받기 #띄어쓰기로 값들 구분되어짐 arr = list(map(int, input().split())) # str list 입력받기 strArr = list(map(str, input().split()) int list -> string arr = [1,2,3] # 방법 1 strArr = list(map(str, arr)) print(strArr) # ['1', '2', '3'] print(''.join(strArr)) # 123 # .. 2022. 3. 2.
반응형