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

42-pknujsp #173

Merged
merged 6 commits into from
May 3, 2024
Merged

42-pknujsp #173

merged 6 commits into from
May 3, 2024

Conversation

pknujsp
Copy link
Collaborator

@pknujsp pknujsp commented Mar 29, 2024

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

๋‹ค๋ฆฌ ๋งŒ๋“ค๊ธฐ

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

1์‹œ๊ฐ„

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

์—ฌ๋Ÿฌ ์„ฌ๋“ค ์ค‘์—์„œ ์–ด๋– ํ•œ ๋‘ ์„ฌ ์‚ฌ์ด๋ฅผ ์ž‡๋Š” ๊ฐ€์žฅ ์งง์€ ๋‹ค๋ฆฌ๋ฅผ ์ฐพ๊ธฐ

  • bridges_queue : ๋‹ค๋ฆฌ๋ฅผ ๋†“์„ ์ง€์ ์„ ๋‹ด๋Š” ํ

1. ์„ฌ ๊ตฌ๋ถ„, ํ•ด์•ˆ๊ฐ€ ํŒŒ์•… : ๋งต ์ „์ฒด๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ, ๊ฐ ์„ฌ์— ID๋ฅผ ํ• ๋‹นํ•˜๊ณ  ํ•ด์•ˆ๊ฐ€๋ฅผ ํ์— ์ถ”๊ฐ€

  • ์ดˆ๊ธฐ ์„ฌ ID๋ฅผ 2๋กœ ์ง€์ • : ๋งต ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆ˜์ •ํ•˜๋ฏ€๋กœ 0, 1๋กœ ID๋ฅผ ํ• ๋‹นํ•˜๋ฉด ๋ฐ”๋‹ค, ์œก์ง€ ์˜๋ฏธ์™€ ๊ฒน์นœ๋‹ค
  1. ํ•œ ์ง€์  ์”ฉ ์ˆœ์„œ๋Œ€๋กœ ๋งต์„ ํ™•์ธํ•œ๋‹ค
  2. ์ˆœํšŒ ์ง€์ ์ด ๋ฐ”๋‹ค or ์ด๋ฏธ ํ™•์ธํ•œ ๊ณณ์ด๋ผ๋ฉด ๋ฐ”๋กœ ๋‹ค์Œ ์ขŒํ‘œ๋กœ ๋„˜์–ด๊ฐ„๋‹ค
  3. ์ฒ˜์Œ ๋ณด๋Š” ์œก์ง€๋ผ๋ฉด ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ ํ•ด๋‹น ์„ฌ ์ „์ฒด ์˜์—ญ์„ BFS๋กœ ํƒ์ƒ‰ํ•œ๋‹ค
    • ๋‹ค์Œ ์˜์—ญ์œผ๋กœ๋Š” ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ๋กœ ์ด๋™ํ•œ๋‹ค
    • ๊ทธ ๊ณผ์ •์—์„œ ํ•ด์•ˆ๊ฐ€ ์ง€์ (์œก์ง€์™€ ๋งž๋‹ฟ์€ ๋ฐ”๋‹ค)์„ bridges_queue ์— ์ถ”๊ฐ€ํ•œ๋‹ค : (์ขŒํ‘œ, ๋‹ค๋ฆฌ๊ธธ์ด 1, ์„ฌ ID) ํ˜•์‹์˜ ์›์†Œ๋ฅผ ์‚ฌ์šฉ
    • ์ง€๋‚˜๊ฐ„ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ๋งต ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๊ฐ’์„ ์„ฌ ID๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค
      • ๋‚˜์ค‘์— ๋‹ค๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด BFS๋ฅผ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ๊ทธ ๋•Œ ๋‹ค๋ฆฌ ํ•˜๋‚˜๊ฐ€ ๋‘ ์„ฌ์„ ์ž‡๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค

2. ๋‹ค๋ฆฌ ๋†“์•„๋ณด๊ธฐ : bridges_queue๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ, ๋‹ค๋ฆฌ์˜ ์ตœ์†Œ ๊ธธ์ด๋ฅผ ๊ตฌํ•œ๋‹ค

  • min_cost : ๋‹ค๋ฆฌ ์ตœ์†Œ ๊ธธ์ด
  1. ํ์—์„œ ๊บผ๋‚ธ ์ง€์ ์˜ ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ์˜ ์ธ์ ‘ ์ง€์ ๋“ค์„ ํ™•์ธํ•œ๋‹ค
  2. ์ธ์ ‘์ ์ด ์•„์ง ๋‹ค๋ฆฌ๊ฐ€ ๋†“์ด์ง€ ์•Š์€ ๋ฐ”๋‹ค๋ผ๋ฉด ํ์— ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค : ๋‹ค๋ฆฌ ๊ธธ์ด๋ฅผ 1 ์ฆ๊ฐ€์‹œ์ผœ์„œ ์ถ”๊ฐ€ํ•จ
  3. ์ด ๋•Œ ํ•ด๋‹น ์ง€์ ์ด ์–ด๋– ํ•œ ์„ฌ๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด, ๋งต ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ด๋‹น ์œ„์น˜ ์›์†Œ๋ฅผ ์„ฌ ID๋กœ ๋ฐ”๊พผ๋‹ค
  4. ์ธ์ ‘์ ์ด ๋‹ค๋ฅธ ์„ฌ๊ณผ ์ด์–ด์ง€๋Š” ๋‹ค๋ฆฌ๊ฐ€ ๋†“์ธ ๊ณณ์ด๋ผ๋ฉด min_cost๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค
    • min_cost = min(min_cost, cost + costs[nr][nc])
    • ํ˜„ ์ง€์  ๊นŒ์ง€์˜ ๋‹ค๋ฆฌ ๊ธธ์ด์™€ ์ธ์ ‘ ์ง€์ ์˜ ๋‹ค๋ฆฌ ๊ธธ์ด์˜ ํ•ฉ์„ ๋น„๊ตํ•˜์—ฌ ๊ฐฑ์‹ ํ•œ๋‹ค
ํ•ด์•ˆ๊ฐ€ ํŒŒ์•… ๊ฒฐ๊ณผ ๋‹ค๋ฆฌ ๊ธธ์ด๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ์ง€์ 
2164_ํ•ด์•ˆ๊ฐ€ 2164_์ตœ์†Œ๊ฑฐ๋ฆฌ

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ ์ตœ์†Œ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋ฉด 3์ด ๋‚˜์˜จ๋‹ค
์˜ค๋ฅธ์ชฝ ์ด๋ฏธ์ง€์—์„œ ๋ณ„ ํ‘œ์‹œ๊ฐ€ ์žˆ๋Š” ์ง€์ ์—์„œ ๊ธธ์ด๊ฐ€ 3์ธ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์„ฌ์„ ์ž‡๋Š” ๋‹ค๋ฆฌ๊ฐ€ ์ด์–ด์ง„๋‹ค

3. min_cost ์ถœ๋ ฅ

์ฝ”๋“œ

map_size = int(input())
matrix = [list(map(int, input().split())) for _ in range(map_size)]

directions = [(1, 0), (0, -1), (-1, 0), (0, 1)]
island_id = 2
costs = [[0] * map_size for _ in range(map_size)]

# ๋‹ค๋ฆฌ๋ฅผ ๋†“์„ ์œ„์น˜๋ฅผ ์ €์žฅํ•  ํ
bridge_queue = deque()

# ์ „์ฒด ๋งต์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์„ฌ์— ID ๋ถ€์—ฌ
for row in range(map_size):
    for col in range(map_size):
        
        if matrix[row][col] != 1:
            continue
            
        # ํ˜„์žฌ ์นธ์„ ์‹œ์ž‘์œผ๋กœ ํ•˜๋Š” ์„ฌ์— ๊ณ ์œ  ID ํ• ๋‹น
        matrix[row][col] = island_id
        queue = deque([(row, col)])
        
        # BFS๋ฅผ ํ†ตํ•ด ๊ฐ™์€ ์„ฌ์ธ ์œก์ง€์— ID ํ• ๋‹น
        while queue:
            r, c = queue.popleft()
            for dr, dc in directions:
                nr, nc = r + dr, c + dc

                if not 0 <= nr < map_size or not 0 <= nc < map_size:
                    continue
                    
                # ๋‹ค๋ฅธ ์„ฌ๊ณผ ์—ฐ๊ฒฐ๋œ ๋ฐ”๋‹ค ์˜์—ญ์— ์ ‘๊ทผํ•˜๋ฉด ์ข…๋ฃŒ
                if 1 < matrix[nr][nc] < island_id:
                    print(1)
                    exit()
                    
                # ์ƒˆ๋กœ์šด ์œก์ง€ ๋ฐœ๊ฒฌ ์‹œ ํ์— ์ถ”๊ฐ€
                if matrix[nr][nc] == 1:
                    queue.append((nr, nc))
                # ์œก์ง€์™€ ๋ฐ”๋กœ ๋งž๋‹ฟ์€ ๋ฐ”๋‹ค ์˜์—ญ์„ ๋‹ค๋ฆฌ ํ›„๋ณด๋กœ ์ถ”๊ฐ€
                elif matrix[nr][nc] == costs[nr][nc] == 0:
                    bridge_queue.append((nr, nc, 1, island_id))
                    
                costs[nr][nc] = 1
                matrix[nr][nc] = island_id
                
        island_id += 1

min_cost = int(1e6)

# ๋‹ค๋ฆฌ ํ›„๋ณด ์œ„์น˜๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ตœ์†Œ ๋‹ค๋ฆฌ ๊ธธ์ด ๊ณ„์‚ฐ
while bridge_queue:
    r, c, cost, curr_island_id = bridge_queue.popleft()
    
    for dr, dc in directions:
        nr, nc = r + dr, c + dc
        
        if not 0 <= nr < map_size or not 0 <= nc < map_size:
            continue
            
        # ์•„์ง ๋‹ค๋ฆฌ๊ฐ€ ๋†“์ด์ง€ ์•Š์€ ๋ฐ”๋‹ค ์˜์—ญ์ด๋ฉด
        # ๋‹ค๋ฆฌ ๊ธธ์ด๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ํ์— ์ถ”๊ฐ€
        if costs[nr][nc] == 0:
            bridge_queue.append((nr, nc, cost + 1, curr_island_id))
            
            costs[nr][nc] = cost + 1
            matrix[nr][nc] = curr_island_id
        # ๋‹ค๋ฅธ ์„ฌ๊ณผ ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฆฌ ์˜์—ญ์— ์ ‘๊ทผํ•˜์˜€๋‹ค๋ฉด ์ตœ์†Œ ๋น„์šฉ์„ ๊ฐฑ์‹ 
        elif matrix[nr][nc] > 1 and matrix[nr][nc] != curr_island_id:
            min_cost = min(min_cost, cost + costs[nr][nc])

print(min_cost)

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

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.

image

์˜ค์˜ˆ!!! ์›ํ๋กœ ํ’€์—ˆ์”๋‹ˆ๋‹ค!!







์ด ๋ฌธ์ œ ๋ฐฑ์กฐ์˜ ํ˜ธ์ˆ˜ ๋ž‘ ๋„ˆ๋ฌด ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒจ์„œ ํ•ด๋‹น ๋กœ์ง ๋– ์˜ฌ๋ฆฌ๋ฉด์„œ ๊ตฌํ˜„ํ•˜๋‹ˆ๊นŒ ๋ฐ”๋กœ ๋™‡! ๋งž๋„ค์š”.

์ค€์„ฑ๋‹˜ ์ €๋ฒˆ์— ์ € ๋ฐฑ์กฐ์˜ ํ˜ธ์ˆ˜ ๋ฌธ์ œ ํฌ๊ธฐํ•˜์‹  ๊ฒƒ ๊ฐ™์€๋ฐ ์ด ๋ฌธ์ œ์˜ ์ƒ์œ„ํ˜ธํ™˜์ธ ๊ฒƒ ๊ฐ™์•„์š” ๋‹ค์‹œ ํ’€์–ด๋ณด๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?!







์ €๋„ ์ค€์„ฑ๋‹˜์ด๋ž‘ ๋กœ์ง ๊ฑฐ์˜ ๋น„์Šทํ•ด์š”! ๊ตฌํ˜„ ๋ฐฉ๋ฒ•๋งŒ ์กฐ๊ธˆ ๋‹ค๋ฅผ ๋ฟ..

์›ํ์— ๋งž์ถ”๋‹ˆ๊นŒ ๋„ˆ๋ฌด ์žฌ๋ฐŒ๋„ค์š” ์บฌํ•˜ํ•˜

import sys
from collections import deque
from copy import deepcopy

def input(): return sys.stdin.readline().rstrip()

N = int(input())
board = []
for _ in range(N):
    _input = input().split()
    board.append(_input)

visited = set()
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

def make_bridge(bridge, board, visited):
    next_bridge = set()

    for row, col in bridge:
        board[row][col] = "1"

        for dir in range(4):
            new_row = row + dx[dir]
            new_col = col + dy[dir]

            if new_row < 0 or new_row >= N: continue
            if new_col < 0 or new_col >= N: continue
            if (new_row, new_col) in visited: continue

            if board[new_row][new_col] == "0":
                next_bridge.add((new_row, new_col))
                visited.add((new_row, new_col))

            if board[new_row][new_col] == "1":
                return None, True

    return next_bridge, False

answer = 10_001
for row in range(N):
    for col in range(N):
        if board[row][col] == "1":
            if ((row, col)) in visited: continue
        
            visited.add((row,col))
            deq = deque([(row, col)])
            bridge_visited = {(row,col),}
            next_bridge = set()

            # ์ดˆ๊ธฐ ์„ฌ์„ ํŒŒ์•…
            while deq:
                now_row, now_col = deq.popleft()

                for dir in range(4):
                    new_row = now_row + dy[dir]
                    new_col = now_col + dx[dir]

                    if new_row < 0 or new_row >= N: continue
                    if new_col < 0 or new_col >= N: continue
                    if (new_row, new_col) in visited: continue

                    if board[new_row][new_col] == "0":
                        next_bridge.add((new_row, new_col))
                        bridge_visited.add((new_row, new_col))

                    if board[new_row][new_col] == "1":
                        deq.append((new_row, new_col))
                        visited.add((new_row, new_col))
                        bridge_visited.add((new_row, new_col))

            count = 1
            new_board = deepcopy(board)
            while True:
                next_bridge, result = make_bridge(next_bridge, new_board, bridge_visited)

                if result:
                    answer = min(answer, count)
                    break

                count += 1

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.

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฒ˜์Œ ๊ณต๋ถ€ํ•  ๋•Œ dfs, bfs๋กœ ์‹œ์ž‘ํ–ˆ๋Š”๋ฐ,, ์—ฌ์ „ํžˆ ์ด๋Ÿฐ map์ด๋‚˜ ๋ฏธ๋กœ ์ฐพ๊ธฐ์—์„œ bfs์™€ dfs๋Š” ์‰ฝ์ง€ ์•Š๋„ค์š” .. ๐Ÿ˜ญ๐Ÿ˜ญ

์ €๋„ ํ๋ฆ„์€ ์ค€์„ฑ๋‹˜๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋กœ์ง์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค!

  1. bfs๋กœ ๋Œ€๋ฅ™์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ & ๋•…๋์ธ ๊ฒฝ์šฐ(์ƒํ•˜์ขŒ์šฐ ์ค‘ 0์ด ์กด์žฌ) ํ์— ๋„ฃ๊ธฐ
  2. ํ์— ๋‹ด๊ธด ๋•…๋๊ณผ ์ฃผ์–ด์ง„ map์„ ๋Œ๋ฉด์„œ ๋‹ค๋ฅธ ๋Œ€๋ฅ™๊ณผ์˜ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ
  3. ์ตœ์†Ÿ๊ฐ’ ์—…๋ฐ์ดํŠธ

๊ทธ๋Ÿฐ๋ฐ,, ๊ณ„์† ํ†ต๊ณผ๊ฐ€ ์•ˆ๋ผ์„œ ์กฐ๊ธˆ ๋” ๊ณ ๋ฏผํ•ด๋ด์•ผํ•  ๊ฒƒ ๊ฐ™์•„์š” ;; ๐Ÿ˜…
๋‹ค์‹œ ๋ฌธ์ œ ๋„์ „ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค-!-!!


island_id += 1

min_cost = int(1e6)
Copy link
Collaborator

@H0ngJu H0ngJu Apr 3, 2024

Choose a reason for hiding this comment

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

์ตœ์†Ÿ๊ฐ’์˜ ์ดˆ๊ธฐ๊ฐ’์„ ๋‹ค๋ฅธ ๋ ˆํผ๋Ÿฐ์Šค์—๋Š” sys ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ maxsize๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๋”๋ผ๊ณ ์š”
์ด๋ฏธ ์•Œ๊ณ  ๊ณ„์‹ค ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ดˆ๊ธฐ๊ฐ’์„ ๋ญ˜๋กœ ๋‘ฌ์•ผํ• ์ง€ ๊ณ ๋ฏผ์ด์‹ค ๋•Œ๋Š” sys.maxsize๋กœ ๋‘๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™์•„์š”!

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