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

4-erase-jeong #17

Merged
merged 2 commits into from
May 25, 2024
Merged

4-erase-jeong #17

merged 2 commits into from
May 25, 2024

Conversation

erase-jeong
Copy link
Collaborator

@erase-jeong erase-jeong commented Apr 4, 2024

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

๋ฏธ๋กœ ํƒ์ƒ‰

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

3~5h

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

1. ๋ฌธ์ œ ์ ‘๊ทผ๋ฒ•

image
๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฏธ๋กœ๊ฐ€ ๋˜์–ด์žˆ์„ ๋•Œ,
๋ฏธ๋กœ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ์ตœ์†Œ์˜ ์นธ ์ˆ˜๋ฅผ ๊ทธ๋ ค๋ณด๋ฉด
์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋…ธ๋ž€์„ ์„ ๋”ฐ๋ผ์„œ ์ด 15์นธ์„ ์ด๋™ํ•˜๊ฑฐ๋‚˜, ๋ถ„ํ™์ƒ‰์„ ์„ ๋”ฐ๋ผ์„œ ์ด 15์นธ์„ ์ด๋™ํ•˜๋Š” ๊ฒƒ์ด ์ตœ์†Œ์˜ ์นธ์ด๋ผ๊ณ  ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿผ ์ด๊ฑธ ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•ด์„œ ํ’€ ๊ฒƒ์ธ๊ฐ€?

( 0์€ ๋ฒฝ์— ํ•ด๋‹นํ•˜๋ฏ€๋กœ ๊ฒ€์€์ƒ‰์œผ๋กœ ํ‘œ์‹œํ•˜์˜€๋‹ค.)
image

  • ๊ฐ€๋Šฅํ•œ ๊ธธ๋“ค์„ ํƒ์ƒ‰ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด, ๊ฐ ์นธ์— ๊ฐˆ๋•Œ๋งˆ๋‹ค ์ด ๋ช‡๊ฐœ์˜ ์นธ์„ ๊ฑด๋„ˆ์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ์ˆซ์ž๋กœ ํ‘œ์‹œํ•ด๋ณด์•˜๋‹ค.

  • 4ํ–‰ 5์—ด๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ 14์นธ, 4ํ–‰ 6์—ด๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ 15์นธ์ด ํ•„์š”ํ•˜๋‹ค.

  • ์ด๋ ‡๋“ฏ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ฐ ์นธ ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์ด ์นธ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ ,
    2์ฐจ์›๋ฐฐ์—ด์˜ Nํ–‰ M์—ด ์— ๋Œ€ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜
    ์žˆ์ง€ ์•Š์„๊นŒ? ๋ผ๋Š” ์•„์ด๋””์–ด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
    ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ๋„์ฐฉ์ ์— ํ•ด๋‹นํ•˜๋Š” 4ํ–‰ 6์—ด์˜ ๊ฐ’์ด 15์ด๋ฏ€๋กœ ๋‹ต์ด 15๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

2. deque ํ™œ์šฉํ•˜๊ธฐ

  • deque๋Š” ์–‘๋ฐฉํ–ฅ์ด ๊ฐ€๋Šฅํ•œ ํ(queue)๋กœ bfs, dfs ๋ฌธ์ œ์—์„œ ๋งŽ์ด ํ™œ์šฉ๋œ๋‹ค.
  • deque๋ฅผ ํ™œ์šฉํ•˜์—ฌ bfs๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณผ ๊ฒƒ์ด๋‹ค.

queue๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ deque()๋ฅผ ์ƒ์„ฑํ•˜๊ณ ,
BFSํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ ๋ฐ›์•„์˜จ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ queue์— ๋”ํ•œ๋‹ค.

queue=deque()
queue.append((x,y))

3. ๋ฌธ์ œ์˜ ์กฐ๊ฑด ๋œฏ์–ด๋ณด๊ธฐ

  • ๋ฏธ๋กœ์—์„œ ์ด๋™ํ•˜๋Š” ํŠน์ง•์„ ์ •๋ฆฌํ•ด๋ณด๋ฉด
    ์ธ์ ‘ํ•œ ์นธ์— ํ•œํ•ด์„œ
  1. (์ธ์ ‘ํ•œ ์นธ์ด ์ด๋™๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด) ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™๊ฐ€๋Šฅํ•˜๋‹ค.

  2. ๋ฒฝ(0)์ด๋ฉด ์ด๋™ ๋ถˆ๊ฐ€๋Šฅ

  3. ์œ„์น˜๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด ์ด๋™ ๋ถˆ๊ฐ€๋Šฅ

์ด๋ ‡๋‹ค.

์—ฌ๊ธฐ์„œ 1, 2 , 3 ์˜ ์กฐ๊ฑด์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž

1) ์ด๋™ํ•  ์ƒ, ํ•˜, ์ขŒ, ์šฐ ๋ฐฉํ–ฅ ์ •์˜

dx=[-1,1,0,0]
dy=[0,0,-1,1]

2) while ๋ฌธ์„ ํ†ตํ•ด์„œ queue์— ๊ฐ’์ด ์žˆ์„ ๋•Œ ๋ฏธ๋กœ ํƒ์ƒ‰ (๋ฏธ๋กœํƒ์ƒ‰ ์กฐ๊ฑด ์ถฉ์กฑํ•˜๋ฉด์„œ)

while queue:  #queue์— ๊ฐ’์ด ์žˆ์œผ๋ฉด ์ˆ˜ํ–‰
       x,y=queue.popleft()   #ํ์˜ ์ œ์ผ ์™ผ์ชฝ์— ์žˆ๋Š” ๊ฐ’์„ ๊บผ๋‚ด๋ฉด์„œ, ๊ทธ ์ขŒํ‘œ(x,y)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฏธ๋กœํƒ์ƒ‰
       for i in range(4):     #ํ˜„์žฌ์œ„์น˜(x,y)์—์„œ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•˜๊ธฐ
            nx = x + dx[i]
            ny = y + dy[i]

            if nx<0 or nx>=N or ny<0 or ny>=M:  #์œ„์น˜๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ
                continue

            if graph[nx][ny]==0: #๋ฒฝ์ด๋ฉด ์ด๋™ ๋ถˆ๊ฐ€๋Šฅ
                continue

            if graph[nx][ny]==1:  #์ด๋™๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
                graph[nx][ny] = graph[x][y]+1
                queue.append((nx,ny))
      

4. ์ด ๋ฌธ์ œ๊ฐ€ bfs ์ธ ์ด์œ ?

์ด ๋ฌธ์ œ๋ฅผ ์™œ bfs๋กœ ํ’€์–ด์•ผํ•˜๋Š”๊ฐ€?

  • ์ด ๋ฌธ์ œ๋Š” ๋ฏธ๋กœํƒ์ƒ‰ ๋ฌธ์ œ๋กœ, ๊ทธ๋ž˜ํ”„์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ๋กœ ํ•ด์„๋  ์ˆ˜ ์žˆ๋‹ค.

  • ์—ฌ๊ธฐ์„œ ๊ทธ๋ž˜ํ”„์˜ ๊ฐ ์ •์ ์€ ๋ฏธ๋กœ์˜ ๊ฐ ์นธ์— ํ•ด๋‹นํ•˜๋ฉฐ, ๊ฐ„์„ ์€ ์นธ๊ณผ ์นธ์‚ฌ์ด์˜ ์ด๋™๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

  • BFS ๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์œ ์šฉํ•œ๋ฐ,

1) ๋‹จ๊ณ„๋ณ„ ํƒ์ƒ‰

  • BFS๋Š” ์‹œ์ž‘ ์ •์ ์—์„œ ๊ฐ€๊นŒ์šด ์ •์ ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ  ๊ทธ ๋‹ค์Œ ๋‹จ๊ณ„์—์„œ ๊ทธ ์ •์ ๋“ค๊ณผ ์ธ์ ‘ํ•œ ์ •์ ๋“ค์„ ๋ฐฉ๋ฌธํ•˜๋Š” ์‹์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ, ์ด ๋ฐฉ์‹์€ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชฉํ‘œ ์ง€์ ๊นŒ์ง€ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๋ณด์žฅํ•œ๋‹ค.

2) ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ณด์žฅ

  • BFS๋Š” ๊ฐ ์ •์ ์„ ์ตœ์ดˆ๋กœ ๋ฐฉ๋ฌธํ•  ๋–„, ์‹œ์ž‘ ์ •์ ์—์„œ ํ•ด๋‹น ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๋ณด์žฅ๋œ๋‹ค.

3) ๋™์ผํ•œ ๊ฑฐ๋ฆฌ์˜ ์ •์  ์ฒ˜๋ฆฌ : BFS๋Š” ์‹œ์ž‘์ ์œผ๋กœ๋ถ€ํ„ฐ ๋™์ผํ•œ ๊ฑฐ๋ฆฌ์— ์žˆ๋Š” ๋ชจ๋“  ์ •์ ์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•œ๋‹ค.

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

  • deque์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ๊ณต๋ถ€ํ–ˆ๋‹ค.

<์ถœ์ฒ˜>

Copy link
Contributor

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

BFS๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ "์ตœ๋‹จ ๊ฒฝ๋กœ"๋กœ ์ฃผํŒŒํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ค‘์š”ํ•œ ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ ๋ณ„๋กœ DFS/BFS ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ์ ํ•ฉํ•œ ๋ฐฉ๋ฒ•์ธ์ง€ ์บ์น˜ํ•ด์„œ ์‘์šฉํ•˜๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์—ด์‹ฌํžˆ ๊ทธ๋ž˜ํ”„ ํ’€์–ด๋ด…์‹œ๋‹ค :)

Comment on lines +5 to +8
graph = []

for _ in range(N):
graph.append(list(map(int, input())))
Copy link
Contributor

Choose a reason for hiding this comment

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

graph = [list(map(int, input())) for _ in range(N)]

๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์š”๋ ‡๊ฒŒ ํ•œ ์ค„๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ต๋‹ˆ๋‹ค :)

Comment on lines +28 to +31
if graph[nx][ny]==0:
continue

if graph[nx][ny]==1:
Copy link
Contributor

Choose a reason for hiding this comment

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

2๋ฒˆ์งธ if๋ฌธ์˜ ์กฐ๊ฑด์„

if graph[nx][ny] != 1:
    continue

์ด๋Ÿฐ ์‹์œผ๋กœ ๋ฐ”๊พธ๋ฉด "๊ฐˆ ์ˆ˜ ์—†๋Š” ์นธ"๊ณผ "์ด๋ฏธ ๊ณ„์‚ฐ๋œ ์นธ" ๋ชจ๋‘๋ฅผ ์ปค๋ฒ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐ‘์—๋Š” ๋ณ„๋„์˜ ์ถ”๊ฐ€ ์กฐ๊ฑด ์—†์ด

graph[nx][ny] = graph[x][y]+1
queue.append((nx,ny))

์ด๋ ‡๊ฒŒ ๋ฐ”๋กœ ์ ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค :)

| 1์ฐจ์‹œ | 2023.03.18 | ๊ทธ๋ฆฌ๋”” | [๊ตฌ๋ช…๋ณดํŠธ](https://school.programmers.co.kr/learn/courses/30/lessons/42885) | [#1](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pull/4)|
| 2์ฐจ์‹œ | 2023.03.23 | DFS/BFS | [์ดŒ์ˆ˜๊ณ„์‚ฐ](https://www.acmicpc.net/problem/2644) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pull/8)|
| 3์ฐจ์‹œ | 2023.03.28 | DP | [ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ](https://www.acmicpc.net/problem/12865) | [#3](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pull/12)|
| 4์ฐจ์‹œ | 2023.03.28 | DP | [๋ฏธ๋กœ ํƒ์ƒ‰](https://www.acmicpc.net/problem/2178) | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pull/17)|
Copy link
Contributor

Choose a reason for hiding this comment

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

์ด์ œ ๋ดค๋Š”๋ฐ ๋ฏธ๋กœ ํƒ์ƒ‰์€ ์นดํ…Œ๊ณ ๋ฆฌ๋กœ DFS/BFS๊ฐ€ ์ ์ ˆํ•ฉ๋‹ˆ๋‹ค :)

@Me-in-U
Copy link
Collaborator

Me-in-U commented May 14, 2024

bfs ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋„ค์š” ์ž˜ ํ‘ธ์‹ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค

Copy link
Collaborator

@wjdheesp44 wjdheesp44 left a comment

Choose a reason for hiding this comment

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

from collections import deque

row, col = map(int, input().split())
grid = []

for i in range(row):
    grid.append(list(map(int, input().strip())))

visited = [[False] * col for _ in range(row)]

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

q = deque()
q.append((0, 0, 1))
visited[0][0] = True

while q:
    cur_x, cur_y, cur_len = q.popleft()
    if cur_x == row - 1 and cur_y == col - 1:
        print(cur_len)
        break;

    for i in range(4):
        next_x = cur_x + dx[i]
        next_y = cur_y + dy[i]
        if next_x >= 0 and next_x < row and next_y >=0 and next_y < col:
            if not visited[next_x][next_y] and grid[next_x][next_y] == 1:
                visited[next_x][next_y] = True
                q.append((next_x, next_y, cur_len + 1))

์ €๋„ bfs๋กœ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค! ์ €๋Š” ํ์— ํƒ์ƒ‰ํ•œ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๊นŒ์ง€ ๋„ฃ์–ด์„œ ์นด์šดํŠธ๋ฅผ ํ–ˆ์–ด์š”!
๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.

@9kyo-hwang 9kyo-hwang merged commit c1697dd into main May 25, 2024
@9kyo-hwang 9kyo-hwang deleted the 4-erase-jeong branch May 25, 2024 08:21
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