-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
41-pknujsp #169
41-pknujsp #169
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import sys
from heapq import *
def input(): return sys.stdin.readline().rstrip()
N, M = map(int, input().split())
board = []
for _ in range(M):
board.append(list(input()))
heap = [(0, 0, 0)]
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
visited = set()
visited.add((0,0))
while heap:
now_cost, now_x, now_y = heappop(heap)
if (now_x, now_y) == (N-1, M-1):
print(now_cost)
break
for dir in range(4):
new_x = now_x + dx[dir]
new_y = now_y + dy[dir]
if new_x < 0 or new_x >= N: continue
if new_y < 0 or new_y >= M: continue
if (new_x, new_y) in visited: continue
if board[new_y][new_x] == "1": heappush(heap, (now_cost+1, new_x, new_y))
else: heappush(heap, (now_cost, new_x, new_y))
visited.add((new_x, new_y))
์ด ๋ฌธ์ ๋ณด์๋ง์ ์ ๋ฒ์ ์ค์ฑ๋์ด ์ถ์ฒํด์ฃผ์ ์ผ์์ผ ์์นจ์ ๋ฐ์ดํธ ๋ฌธ์ ์๊ฐ๋์ ๋ฐ๋ก ํ์ ๋ ์ฌ๋ฆด ์ ์์์ต๋๋ค!!
์ธ์ฌ์ดํธ ๊ฐ์ฌํฉ๋๋ค~~~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ฒ PR๋ก ๋ค์ต์คํธ๋ผ ๋ค์ ๋ณต์ตํ๊ณ ๊ฐ๋๋ค !
๋ค์ต์คํธ๋ผ๋ฅผ ๊ตฌํํ ๋ค๋ฅธ ๋ ํผ๋ฐ์ค๋ฅผ ๋ณด๋ ํ์คํ heap์ผ๋ก ๊ตฌํํ๊ฒ ์ฝ๋๋ ์งง๊ณ ์๊ฐ๋ณต์ก๋๋ ์ค์ด๋๋ค์ฉ ํ๊ท๋ ๋ฒจ๋ง-ํฌ๋ pr๊ณผ ํจ๊ป ์ต๋จ๊ฑฐ๋ฆฌ ์๊ณ ๋ฆฌ์ฆ ๊ณต๋ถํ๊ณ ๊ฐ๋๋ท !!
์ค์ฑ๋ ์๋ง ํฉ๊ฒฉ ์ถํ๋๋ฆฝ๋๋ค ~~ ๐๐
if r == target_r and c == target_c: | ||
print(cost) | ||
break | ||
if visited[r][c]: | ||
continue | ||
|
||
visited[r][c] = True | ||
|
||
for dr, dc in drc: | ||
nr = r + dr | ||
nc = c + dc | ||
|
||
if not 0 <= nr < R or not 0 <= nc < C: | ||
continue | ||
if visited[nr][nc]: | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ค์ฑ๋ ์ฝ๋ ๋ณผ ๋๋ง๋ค ๋๋ผ๋๊ฑด๋ฐ continue๋ฌธ์ ์ ํ์ฉํ์๋ ๊ฒ ๊ฐ์ต๋๋ค
๋ง์ฝ์ visited[ ][ ]๊ฐ False์ธ ๊ฒฝ์ฐ์๋ง 22~31๋ฒ์ค์ ์คํํ๋๋ก ํ๋ฉด
์ ๋ if-else๋ก ๊ตฌํํ์ ๊ฒ ๊ฐ์๋ฐ continue๋ฅผ ์ฌ์ฉํ๋๊น ๋ณต์กํ ์ธ๋ดํธ๊น์ง ์๊ฐ ์ํด๋ ๋๋ ์ ๋์๋ ์ด๊ฒ ๋ ๊น๋ํด ๋ณด์ด๋ ๊ฒ ๊ฐ๊ธฐ๋ ํ๋ค์ ! ๐
๐ ๋ฌธ์ ๋งํฌ
์๊ณ ์คํ
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
โจ ์๋ ์ฝ๋
1. ์ค๋ณต ๋ฐฉ๋ฌธ ๋ฐฉ์ง
visited
๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํด ๊ฐ ์์น์ ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ๊ธฐ๋ก2. ์ต์ ํ์ ์ฌ์ฉํด ์ํ์ข์ฐ ํ์
3. ํ์ : ๋ชฉ์ ์ง์ ๋์ฐฉํ ๋ ๊น์ง ํ์ํ๋ค
(๋ฒฝ ํ๊ดด ํ์, ์์น)
๋ฅผ ์์๋ก ํ์ฌ ํ์ ์ถ๊ฐ/์ญ์ ํ๋ฉฐ ํ์ํ๋ค(์๋ก์ด ๋น์ฉ, ์๋ก์ด ์์น)
๋ก ์์๋ฅผ ์ถ๊ฐํ๋ค๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ