Skip to content
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

Merged
merged 5 commits into from
May 3, 2024
Merged

41-pknujsp #169

merged 5 commits into from
May 3, 2024

Conversation

pknujsp
Copy link
Collaborator

@pknujsp pknujsp commented Mar 25, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์•Œ๊ณ ์ŠคํŒŸ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฒฝ์„ ์ตœ์†Œํ•œ์œผ๋กœ ๋ถ€์ˆ˜๋ฉด์„œ ๋ชฉ์ ์ง€๊นŒ์ง€ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ

1. ์ค‘๋ณต ๋ฐฉ๋ฌธ ๋ฐฉ์ง€

  • visited ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ ์œ„์น˜์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ธฐ๋ก

2. ์ตœ์†Œ ํž™์„ ์‚ฌ์šฉํ•ด ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰

  • ํ˜„์žฌ๊นŒ์ง€ ๋ฐœ๊ฒฌ๋œ ๊ฒฝ๋กœ ์ค‘์—์„œ ๊ฐ€์žฅ ์ ์€ ๋น„์šฉ(์—ฌ๊ธฐ์„œ๋Š” ๋ฒฝ์„ ๊ฐ€์žฅ ์ ๊ฒŒ ๋ถ€์ˆœ ํšŸ์ˆ˜)์„ ๊ฐ€์ง„ ๊ฒฝ๋กœ๋ฅผ ๋จผ์ € ์ฒ˜๋ฆฌํ•œ๋‹ค
  • ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

3. ํƒ์ƒ‰ : ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•  ๋•Œ ๊นŒ์ง€ ํƒ์ƒ‰ํ•œ๋‹ค

  • (๋ฒฝ ํŒŒ๊ดด ํšŸ์ˆ˜, ์œ„์น˜) ๋ฅผ ์›์†Œ๋กœ ํ•˜์—ฌ ํž™์— ์ถ”๊ฐ€/์‚ญ์ œํ•˜๋ฉฐ ํƒ์ƒ‰ํ•œ๋‹ค
  • ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•˜๋ฉด ๋ฒฝ ํŒŒ๊ดด ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•จ
  1. ์ฃผ๋ณ€ ์œ„์น˜ ํ™•์ธํ•˜๊ณ , ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด ํž™์— ์ถ”๊ฐ€
    • ํ˜„์žฌ ์œ„์น˜์—์„œ ์ƒํ•˜์ขŒ์šฐ์— ์žˆ๋Š” ์œ„์น˜๋ฅผ ํ™•์ธํ•œ๋‹ค
    • ์ด ๋•Œ ๋‹ค์Œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์œ„์น˜์— ๋Œ€ํ•ด์„œ ํž™์— ์ถ”๊ฐ€ํ•˜๊ณ  ํƒ์ƒ‰ํ•œ๋‹ค
    • ๋ฏธ๋กœ์˜ ๋ฒ”์œ„ ์•ˆ์— ์žˆ์–ด์•ผ ํ•˜๊ณ 
    • ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์œ„์น˜์—ฌ์•ผ ํ•œ๋‹ค
  2. ํž™์— ์ถ”๊ฐ€ํ•  ๋•Œ๋Š”, (์ƒˆ๋กœ์šด ๋น„์šฉ, ์ƒˆ๋กœ์šด ์œ„์น˜) ๋กœ ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค
    1. ์ƒˆ๋กœ์šด ๋น„์šฉ : ํ˜„ ์œ„์น˜๊นŒ์ง€์˜ ๋ฒฝ ํŒŒ๊ดด ํšŸ์ˆ˜ + ํ•ด๋‹น ์œ„์น˜๊ฐ€ ๋ฒฝ์ด๋ผ๋ฉด 1, ๋นˆ ๋ฐฉ์ด๋ผ๋ฉด 0
    2. ์ƒˆ๋กœ์šด ๋ฒฝ ํŒŒ๊ดด ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•จ
C, R = map(int, stdin.readline().split())
arr = [list(map(int, list(stdin.readline().strip()))) for _ in range(R)]

drc = ((1,0),(-1,0),(0,1),(0,-1))
visited = [[False] * C for _ in range(R)]
heap = [(0, 0, 0)]
target_r = R - 1
target_c = C - 1

while heap:
    cost, r, c = heappop(heap)

    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
            
        heappush(heap, (cost + arr[nr][nc], nr, nc))

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Member

@tgyuuAn tgyuuAn left a 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))

์ด ๋ฌธ์ œ ๋ณด์ž๋งˆ์ž ์ €๋ฒˆ์— ์ค€์„ฑ๋‹˜์ด ์ถ”์ฒœํ•ด์ฃผ์‹  ์ผ์š”์ผ ์•„์นจ์˜ ๋ฐ์ดํŠธ ๋ฌธ์ œ ์ƒ๊ฐ๋‚˜์„œ ๋ฐ”๋กœ ํž™์„ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!!

์ธ์‚ฌ์ดํŠธ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~~~~

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ฒˆ PR๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ ๋‹ค์‹œ ๋ณต์Šตํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค !

๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๊ตฌํ˜„ํ•œ ๋‹ค๋ฅธ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ณด๋‹ˆ ํ™•์‹คํžˆ heap์œผ๋กœ ๊ตฌํ˜„ํ•œ๊ฒŒ ์ฝ”๋“œ๋„ ์งง๊ณ  ์‹œ๊ฐ„๋ณต์žก๋„๋„ ์ค„์–ด๋“œ๋„ค์šฉ ํƒœ๊ทœ๋‹˜ ๋ฒจ๋งŒ-ํฌ๋“œ pr๊ณผ ํ•จ๊ป˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€ํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ท !!

์ค€์„ฑ๋‹˜ ์†Œ๋งˆ ํ•ฉ๊ฒฉ ์ถ•ํ•˜๋“œ๋ฆฝ๋‹ˆ๋‹ค ~~ ๐ŸŽ‰๐ŸŽ‰

Comment on lines +16 to +31
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
Copy link
Collaborator

@H0ngJu H0ngJu Mar 29, 2024

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๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ๊นŒ ๋ณต์žกํ•œ ์ธ๋ดํŠธ๊นŒ์ง€ ์ƒ๊ฐ ์•ˆํ•ด๋„ ๋˜๋‹ˆ ์ œ ๋ˆˆ์—๋Š” ์ด๊ฒŒ ๋” ๊น”๋”ํ•ด ๋ณด์ด๋Š” ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๋„ค์š” ! ๐Ÿ˜Ž

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants