백준 7576 - 토마토
import sys from collections import deque input = sys.stdin.readline M, N = map(int, input().split()) arr = [list(map(int, input().split())) for _ in range(N)] dir = [ [1,0], [-1,0], [0,1], [0,-1] ] tomatoes = deque([]) for i in range(N): for j in range(M): if arr[i][j] == 1: tomatoes.append([i,j]) while tomatoes: y,x = tomatoes.popleft() nextVal = arr[y][x] + 1 for d in dir: ny = y + d[0] nx = x..
더보기
프로그래머스 여행경로 - 백트래킹
def dfs(ans, routes, pointNum, answer, isFinished): if len(ans) == pointNum: if not isFinished[0]: for i in ans: answer.append(i) isFinished[0] = True return nexts = isPossible(ans[-1], routes) for next in nexts: if not isFinished[0]: next[1] = True ans.append(next[0]) dfs(ans, routes, pointNum, answer, isFinished) # 티켓 사용여부 False로 돌려놓음 next[1] = False ans.pop() def isPossible(start, routes): re..
더보기