백준 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..
더보기
릿코드 37- Sudoku Solver, nQueen 다시풀기
def dfs(board, empty, count): global isEnd if count == len(empty): for i in board: print(i) isEnd = True return y,x = empty[count] nums = possible(board, y, x) for num in nums: if not isEnd: board[y][x] = num dfs(board, empty, count+1) board[y][x] = 0 def possible(board, y, x): nums = [True for i in range(10)] for i in range(9): nums[board[y][i]] = False nums[board[i][x]] = False y = (y//3) * 3 ..
더보기
백준 17114 - 미세먼지 확산
빡구현 문제였다. 카카오 기출 카드짝 맞추기도 그렇고 이번문제도 그렇고 이것저것 많이 풀어보다보니 확실히 구현문제 실력이 성장한 것 같다. def spread(arr, height, width, cleaner) : spreadList = [] direction = { 0:[0,-1], 1:[0,1], 2:[-1,0], 3:[1,0] } newArr = [[ 0 for _ in range(width)] for i in range(height)] newArr[cleaner[0]][0] = -1 newArr[cleaner[1]][0] = -1 for y in range(height): for x in range(width): if arr[y][x] == -1 : continue if arr[y][x] != 0..
더보기