Skip to content

Commit

Permalink
Merge pull request #41 from AlgoLeadMe/12-alstjr7437
Browse files Browse the repository at this point in the history
12-alstjr7437
  • Loading branch information
alstjr7437 authored Mar 3, 2024
2 parents f43b61b + 499bae6 commit b5173f4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
88 changes: 88 additions & 0 deletions alstjr7437/BFS/백조의-호수.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from collections import deque
import sys

dx = [0,0,-1,1]
dy = [1,-1,0,0]
r, c = map(int, input().split())

water1 = deque()
water2 = deque()
visited = [[False] * c for _ in range(r)]

swan1 = deque()
swan2 = deque()
visited2 = [[False] * c for _ in range(r)]

# 1. 전체 호수 만들기
river = []
for i in range(r):
river.append(list(input()))

day_count = 0

# 2. 백조 위치 찾기와 물 위치 찾기
swan = []
for x in range(c):
for y in range(r):
if river[y][x] == "L":
swan.append([y,x])
river[y][x] = "."
if river[y][x] == ".":
water1.append([y, x])
visited[y][x] = True

# 3. 백조를 큐에 추가
swan1.append(swan[0])
visited2[swan[0][0]][swan[0][1]] = True

# 4. 하루에 한번씩 X 없애기(얼음 없애기)
def break_ice():
# print("-------")
while water1:
y, x = water1.popleft()
# print(y, x, water1, water2)
river[y][x] = "."
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= c or ny < 0 or ny >= r:
continue
if not visited[ny][nx]:
if river[ny][nx] == ".":
water1.append([ny,nx])
else :
water2.append([ny,nx])
visited[ny][nx] = True

# 5. 백조가 만나는지 체크하기
def check_swan():
# print("-------")
while swan1 :
y, x = swan1.popleft()
# print(x,y,swan1, swan2)
if y == swan[1][0] and x == swan[1][1]:
print(day_count)
sys.exit()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= c or ny < 0 or ny >= r:
continue
if not visited2[ny][nx]:
if river[ny][nx] == "." and visited2[ny][nx] == False:
swan1.append([ny,nx])
else :
swan2.append([ny,nx])
visited2[ny][nx] = True
return False

# 만날때까지 돌리기
while 1:
break_ice()
water1 = water2
water2 = deque()

check_swan()
swan1 = swan2
swan2 = deque()
day_count += 1
2 changes: 1 addition & 1 deletion alstjr7437/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
| 9차시 | 2024.02.17 | 그리디 | <a href="https://www.acmicpc.net/problem/2138">전구와 스위치</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/29 |
| 10차시 | 2024.02.20 | BFS/DFS | <a href="https://www.acmicpc.net/problem/11725">트리의 부모 찾기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/33 |
| 11차시 | 2024.02.23 | 해시 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/42579">베스트 앨범</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/39 |

| 12차시 | 2024.02.26 | BFS | <a href="https://www.acmicpc.net/problem/3197">백조의 호수</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/41 |

0 comments on commit b5173f4

Please sign in to comment.