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

12-alstjr7437 #41

Merged
merged 3 commits into from
Mar 3, 2024
Merged

12-alstjr7437 #41

merged 3 commits into from
Mar 3, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Feb 26, 2024

πŸ”— 문제 링크

백쑰의 호수

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

4μ‹œκ°„ (버린 μ‹œκ°„ 2μ‹œκ°„ 30λΆ„)
ν”Œλ ˆ 문제 치고 μ‰½λ‹€κΈΈλž˜ 도전을 ν–ˆλŠ”λ°...
μ‹œκ°„ μ΄ˆκ³Όλž‘ 생각을 μ•ˆν•˜λ©΄ 잘 ν’€λ ΈλŠ”λ° μ‹œκ°„ μ΄ˆκ³Όκ°€ λœ¨λ‹ˆκΉŒ 머리가 βšͺ️이 λμŠ΅λ‹ˆλ‹€..

🧐 ν‹€λ¦° λΆ€λΆ„

μš°μ„  λ¬Έμ œμ—μ„œ ν•˜λ£¨μ— ν•˜λ‚˜μ”© μ–ΌμŒμ„ μ§€μ›Œλ‚˜κ°€λŠ” 뢀뢄을 돌리고
μœ„μ— 뢀뢄을 돌리고 λ‚œ 후에 λ°±μ‘°κ°€ λ§Œλ‚  수 μžˆλŠ”μ§€ ν…ŒμŠ€νŠΈν•˜λ©΄ λ˜κ² λ”λΌκ΅¬μš”!




κ·Έλž˜μ„œ λ‚˜μ˜¨λΆ€λΆ„μ΄ 맀번 visitedλ₯Ό 맀번 μ΄ˆκΈ°ν™”ν•΄μ„œ 맀일맀일 λŒλ¦¬λŠ” λΆ€λΆ„μ΄μ˜€μŠ΅λ‹ˆλ‹€!

  1. 전체 호수 λ°›μ•„μ˜€κΈ°

  2. ν•˜λ‚˜μ”© μ–ΌμŒ μ—†μ• λŠ” ν•¨μˆ˜ λ§Œλ“€κΈ°

    • .μ΄λ‚˜ LλΆ€λΆ„μ—μ„œ Xλ₯Ό λ§Œλ‚˜λ©΄ ν•œκ°œ λΆ€μˆ˜κΈ°
    • ν•œκ°œ ꡬ뢄은 visited λ°°μ—΄λ‘œ
  3. λ°±μ‘° λ§Œλ‚˜λŠ”μ§€ μ²΄ν¬ν•˜λŠ” ν•¨μˆ˜ λ§Œλ“€κΈ°

    • BFS둜 L을 λ§Œλ‚˜λŠ”μ§€ 큐에 λ„£μœΌλ©΄μ„œ μ­‰ μ²΄ν¬ν•˜κΈ°
    • λ§Œλ‚˜λ©΄ day_countλ₯Ό 좜λ ₯ν•˜κ³  sys.exit()둜 μ½”λ“œ μ’…λ£Œ
  4. while문으둜 2~3λ²ˆμ„ κ³„μ†ν•΄μ„œ λŒμ•„κ°€λ©΄μ„œ river μ΅œμ‹ ν™” ν•΄μ£ΌκΈ°

μœ„μ™€ 같이 μ–΄λŠμ •λ„ μ–΄λ–»κ²Œ λŒμ•„κ°€μ•Όν• μ§€ λ§Œλ“€μ–΄μ§€κ³  μ•„λž˜μ™€ 같이 μ½”λ“œλ₯Ό μ§―μŠ΅λ‹ˆλ‹€!

from collections import deque
import sys

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

r, c = map(int, input().split())

# 1. 전체 호수 λ§Œλ“€κΈ°
river = []

for i in range(r):
    river.append(list(input()))

day_count = 0

# 2. ν•˜λ£¨μ— ν•œλ²ˆμ”© X μ—†μ• κΈ°(μ–ΌμŒ μ—†μ• κΈ°)
def break_ice(river):
    global day_count
    visited = [[False] * c for _ in range(r)]
    for x in range(c):
        for y in range(r):
            if river[y][x] == "." or river[y][x] == "L" :
                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 river[ny][nx] == "X" and visited[y][x] == False:
                        river[ny][nx] = "."
                        visited[ny][nx] = True
    
    day_count += 1
    return river
         

# 3. λ°±μ‘°κ°€ λ§Œλ‚˜λŠ”μ§€ μ²΄ν¬ν•˜κΈ°
def check_swan(river):
    queue = deque()
    meets = [[False] * c for _ in range(r)]

    
    # L을 처음 λ„£λŠ” λΆ€λΆ„
    for x in range(c):
        for y in range(r):
            if river[y][x] == "L":
                swan_x = x
                swan_y = y

    queue.append([swan_y,swan_x])
    meets[swan_y][swan_x] = True

    # Lκ³Ό L이 λ§Œλ‚˜λŠ”μ§€ μ°ΎλŠ” λΆ€λΆ„
    while queue:
        y,x = queue.popleft()
        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 meets[ny][nx] == False and river[ny][nx] == "L":
                print(day_count)
                sys.exit()
            if meets[ny][nx] == False and river[ny][nx] == ".":
                queue.append([ny, nx])
                meets[ny][nx] = True
    return False

while 1:
    check_swan(river)
    river = break_ice(river)

근데 μœ„μ™€ 같이 ν•˜κ²Œλ˜λ©΄ μ‹œκ°„μ΄ˆκ³Όκ°€ λ‚˜μ˜€λ”λΌκ΅¬μš”
λ‚˜μ™€μ„œ λ‹€μ‹œ μƒκ°ν•΄λ³΄λ‹ˆ
μ–ΌμŒμ„ λΆ€μˆ λ•Œλ„ river의 λͺ¨λ“  뢀뢄을 λŒλ¦¬λ©΄μ„œ μ‹œκ°„ πŸ†™
λ°±μ‘° λ§Œλ‚˜λŠ”μ§€ μ²΄ν¬ν• λ•Œλ„ λͺ¨λ“  뢀뢄을 λ‹€μ‹œ ν™•μΈν•΄μ•Όν•΄μ„œ μ‹œκ°„ πŸ†™
μ΄λ ‡κ²Œ μ—¬λŸ¬κ°€μ§€ μ‹œκ°„ 초과λ₯Ό ν• λ§Œν•œ μ½”λ“œλ₯Ό μž‘μ„±ν–ˆμ—ˆμŠ΅λ‹ˆλ‹€.

κ°€λ‘œ μ„Έλ‘œ 길이 1500인데 그럼 호수의 μ΅œλŒ€ 크기가 λŒ€λž΅ 200만인데
200λ§Œμ„ 계속 λ°˜λ³΅ν•  μˆ˜λ„..?




⭐️ μ •λ‹΅

μœ„μ™€ 같이 1, 2, 3, 4둜
μ–ΌμŒμ„ λΆ€μˆ˜κ³  λ°±μ‘°κ°€ λ§Œλ‚˜λŠ”μ§€ μ²΄ν¬ν•˜λŠ” 뢀뢄은 λ˜‘κ°™μ§€λ§Œ
ν•œλ²ˆ BFSλ₯Ό ν•œ 뢀뢄은 λ‹€μ‹œ 쓸일이 μ—†κ³  μ‹œκ°„ λ‚­λΉ„λ§Œ λμŠ΅λ‹ˆλ‹€!!

κ·Έλž˜μ„œ μƒκ°ν•œ 뢀뢄이 μ–‘λ°©ν–₯ 큐λ₯Ό μ΄μš©ν•΄ μž¬μ‚¬μš©μ„ μ—†μ• λ©΄ λμŠ΅λ‹ˆλ‹€!




🧊 μ–ΌμŒ κΉ¨μ§€λŠ” λΆ€λΆ„

index 0 번 1 번 2 번
0 . L . L . L
1 . . . . . .
2 X X . . . .
3 X X X X . .
4 X X X X . .
5 X X . . . .
6 . . . . . .
7 . L . L . L

μœ„ ν‘œμ™€ 같이 μ›€μ§κ²Œ λ˜λŠ”λ°
λ”°λ‘œ .뢀뢄을 λ‹€μ‹œ 돌릴 ν•„μš” 없이
Xλ©΄ 2번째 큐에 λ„£μ–΄μ£Όλ©΄ λμŠ΅λ‹ˆλ‹€.

  1. .이 λ‚˜μ˜€λ©΄ water1에 κ·ΈλŒ€λ‘œ appendν•΄μ„œ ν™•μž₯
  2. Xκ°€ λ‚˜μ˜€λ©΄ water2에 λ„£μ–΄μ„œ μ €μž₯ν•΄λ‘ 
  3. .이 λ‹€ λŒμ•„κ°€κ³  λ‚˜λ©΄ water2μ—λŠ” Xμ˜€λ˜κ±° 쀑 μΈμ ‘ν•œκ²Œ 담아짐
  4. water1κ³Ό water2λ₯Ό κ΅μ²΄ν•˜κ³  2λ₯Ό λ‹€μ‹œ λΉ„μ›Œμ€Œ
  5. 1~4 반볡

μœ„ ν‘œ 예제λ₯Ό λ“€λ©΄
water1 -> 0, 1, 6, 7
water2 -> 2, 5

water1 -> 2, 5
water2 -> 3, 4

water 1 -> 3, 4

μ΄λ ‡κ²Œ ν•΄μ„œ λͺ¨λ“  μ–ΌμŒμ΄ κΉ¨μ§€κ²Œ λ©λ‹ˆλ‹€!!!




🦒 λ°±μ‘° λ§Œλ‚˜λŠ”μ§€ μ²΄ν¬ν•˜κΈ°

이뢀뢄도 μœ„μ™€ λ˜‘κ°™μ€ λ°©μ‹μœΌλ‘œ κ°€μ§€λ§Œ 처음 넣은 λ°±μ‘°λΆ€λΆ„λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€.
그리고 μœ„μ™€ 같이 μ§„ν–‰ν•˜λ©΄μ„œ λ°±μ‘°κ°€ 0,1에 μžˆμœΌλ‹ˆ 0,1을 popν•΄μ£Όκ³  μ£Όλ³€ 뢀뢄을 λ„£μ–΄μ€λ‹ˆλ‹€.

swan1 -> 0, 1
swan2 -> 2

swan1 -> 2
swan2 -> 3

swan1 -> 3, 4, 5, 6, 7
λͺ¨λ‘ .으둜 swan1에 λ‹€ push되고 λ‹€λ₯Έ 백쑰의 μ’Œν‘œλ₯Ό λ§Œλ‚˜κ²Œ λ©λ‹ˆλ‹€.




πŸ’» μ΅œμ’… μ½”λ“œ

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

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

μ–΄λ–»κ²Œ κ΅¬ν˜„κΉŒμ§€λŠ” μ‰½κ²Œ ν’€λ ΈλŠ”λ° μ‹œκ°„μ΄ˆκ³Ό λ©”λͺ¨λ¦¬ 초과 계산을 μ•ˆν•˜λ‹ˆκΉŒ 정말 λ©˜λΆ•μ΄λ„€μš”...
κ·Έλž˜λ„ μ–‘λ°©ν–₯ 큐λ₯Ό μ΄μš©ν•΄μ„œ μž¬μ‚¬μš© μ‹œκ°„μ„ μ€„μ΄λŠ” 뢀뢄을 μ—°μŠ΅ν•  수 μžˆμ–΄μ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€!

@9kyo-hwang
Copy link

ν”Œλž˜ 5 μ‹€-μž‡

@9kyo-hwang
Copy link

ν™•μ‹€νžˆ λ‹€λ₯Έ μ‚¬λžŒλ“€μ΄ μ œμΆœν•œ μ½”λ“œ λ³΄λ‹ˆκΉŒ 큐 2개λ₯Ό μ“°λŠ” 풀이가 λ§Žλ”λΌκ΅¬μš”.

제 경우 μœ λ‹ˆμ˜¨ νŒŒμΈλ“œλ₯Ό μ΄μš©ν•΄μ„œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€. 백쑰의 호수 제 PR에 κ·Έλ¦Ό 자료 같이 μ²¨λΆ€ν•΄λ†¨μœΌλ‹ˆ ν•œ 번 ν™•μΈν•΄λ³΄μ„Έμš”~

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

헐 λ‹€μŒ bfs 큐둜 λ„˜κ²¨μ£ΌλŠ”κ±΄ 생각 λͺ»ν–ˆλŠ”데..
직접 ν’€μ–΄λ³Ό 엄두가 μ•ˆλ‚˜μ„œ μ„€λͺ… λ³΄λ©΄μ„œ, μ½”λ“œλ₯Ό λ”°λΌμΉ˜λ©΄μ„œ λ΄€μŠ΅λ‹ˆλ‹Ή..
κΌΌκΌΌν•˜κ²Œ PR μ˜¬λ €μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€!

@alstjr7437 alstjr7437 removed the request for review from fnzksxl March 2, 2024 08:40
Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

λ³΄λ‹ˆκΉŒ queue 4개 μ¨μ„œ ν‘ΈλŠ” μ‚¬λžŒλ„ μžˆλ”λΌκ΅¬μš”.

from collections import deque
import sys
input = sys.stdin.readline



def waterMelt() :
    while waterPos :
        y,x = waterPos.popleft()
        maps[y][x] = '.'

        for dy,dx in dir :
            ny,nx = y+dy, x+dx
            if 0<=ny < r and 0 <= nx <c and not waterCheck[ny][nx] :
                if maps[ny][nx] == '.' :
                    waterPos.append((ny,nx))
                elif maps[ny][nx] == 'X' :
                    nextWaterPos.append((ny,nx))
                waterCheck[ny][nx] = True

def findSwan() :
    while swanPos :
        y,x = swanPos.popleft()

        if y == endY and x == endX :
            return True
    

        for dy,dx in dir :
            ny,nx = y + dy, x + dx
            if 0<= ny < r and 0 <= nx < c and not swanCheck[ny][nx] :
                if maps[ny][nx] == '.':
                    swanPos.append((ny,nx))
                elif maps[ny][nx] == 'X' :
                    nextSwanPos.append((ny,nx))
                swanCheck[ny][nx] = True
    return False


r,c = map(int, input().split())
maps = [list(input().strip()) for _ in range(r)]
swanCheck = [[False] * c for _ in range(r)]
waterCheck = [[False] * c for _ in range(r)]
dir = [[1,0], [-1,0], [0,1], [0,-1]]

swanPos = deque()
nextSwanPos = deque()
waterPos = deque()
nextWaterPos = deque()


for i in range(r):
    for j in range(c):
        if maps[i][j] =='L' :
            if not swanPos :
                swanPos.append((i,j))
                swanCheck[i][j] = True
            else :
                endY, endX = i,j
            maps[i][j] = '.'

            waterPos.append((i,j))
            waterCheck[i][j] = True
        elif maps[i][j] == '.':
            waterPos.append((i,j))
            waterCheck[i][j] = True


days =0
while True :
    waterMelt()
    if findSwan() : break

    swanPos = nextSwanPos
    waterPos = nextWaterPos
    nextSwanPos = deque()
    nextWaterPos = deque()

    days+=1

print(days)

@alstjr7437
Copy link
Member Author

λ³΄λ‹ˆκΉŒ queue 4개 μ¨μ„œ ν‘ΈλŠ” μ‚¬λžŒλ„ μžˆλ”λΌκ΅¬μš”.
@wkdghdwns199 κ·ΈλŸΌμš”~ 저도 큐 4개 μ»μŠ΅λ‹ˆλ‹€

@alstjr7437 alstjr7437 merged commit b5173f4 into main Mar 3, 2024
2 checks passed
@alstjr7437 alstjr7437 deleted the 12-alstjr7437 branch March 3, 2024 11:12
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