Algorithms / 백준 2178번 파이썬
10376 단어 pythonAlgorithms백준Algorithms
링크
https://www.acmicpc.net/problem/2178
풀이
코드
from collections import deque
# for i in range(n):
# maze.append(list(input()))
# for j in range(m):
# maze[i][j] = int(maze[i][j])
def main():
n, m = map(int, input().split())
maze = []
# getting inputs, spliting and coverting to int type
for _ in range(n):
maze.append(list(map(int, input())))
# left, down, right, up
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
attendance = [[0] * m for _ in range(n)]
q = deque([(0, 0)]) # deque([(0, 0)])
# assign the first location as 1 using given information from the problem.
attendance[0][0] = 1
while q:
x, y = q.popleft()
# print out the final value once it gets the destination.
if x == n-1 and y == m-1:
print(attendance[x][y])
for i in range(4):
# initializing next steps to explore further each direction.
nx, ny = x + dx[i], y + dy[i]
# when new x and y locations are inside of the maze
if 0 <= nx < n and 0 <= ny < m:
# next location is not explored and not a wall
if attendance[nx][ny] == 0 and maze[nx][ny] == 1:
# adding up current steps and + 1 as well
attendance[nx][ny] = attendance[x][y] + 1
q.append((nx, ny))
def print2DList(maze):
for i in range(len(maze)):
for j in range(len(maze[0])):
print(maze[i][j], end=' ')
if __name__ == '__main__':
main()
Algorithms / 백준 2178번 파이썬
Author And Source
이 문제에 관하여(Algorithms / 백준 2178번 파이썬), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@kon6443/백준-BaekJoon-2178저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)