-
Notifications
You must be signed in to change notification settings - Fork 2
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
42-pknujsp #173
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ค์!!! ์ํ๋ก ํ์์๋๋ค!!
์ด ๋ฌธ์ ๋ฐฑ์กฐ์ ํธ์ ๋ ๋๋ฌด ๋น์ทํ๊ฒ ์๊ฒจ์ ํด๋น ๋ก์ง ๋ ์ฌ๋ฆฌ๋ฉด์ ๊ตฌํํ๋๊น ๋ฐ๋ก ๋! ๋ง๋ค์.
์ค์ฑ๋ ์ ๋ฒ์ ์ ๋ฐฑ์กฐ์ ํธ์ ๋ฌธ์ ํฌ๊ธฐํ์ ๊ฒ ๊ฐ์๋ฐ ์ด ๋ฌธ์ ์ ์์ํธํ์ธ ๊ฒ ๊ฐ์์ ๋ค์ ํ์ด๋ณด๋ ๊ฑด ์ด๋จ๊น์?!
์ ๋ ์ค์ฑ๋์ด๋ ๋ก์ง ๊ฑฐ์ ๋น์ทํด์! ๊ตฌํ ๋ฐฉ๋ฒ๋ง ์กฐ๊ธ ๋ค๋ฅผ ๋ฟ..
์ํ์ ๋ง์ถ๋๊น ๋๋ฌด ์ฌ๋ฐ๋ค์ ์บฌํํ
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
There was a problem hiding this 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๋ ์ฝ์ง ์๋ค์ .. ๐ญ๐ญ
์ ๋ ํ๋ฆ์ ์ค์ฑ๋๊ณผ ๋น์ทํ๊ฒ ๋ก์ง์ ๊ตฌํํ์ต๋๋ค!
- bfs๋ก ๋๋ฅ์ ๊ตฌ๋ถํ๊ธฐ & ๋ ๋์ธ ๊ฒฝ์ฐ(์ํ์ข์ฐ ์ค 0์ด ์กด์ฌ) ํ์ ๋ฃ๊ธฐ
- ํ์ ๋ด๊ธด ๋ ๋๊ณผ ์ฃผ์ด์ง map์ ๋๋ฉด์ ๋ค๋ฅธ ๋๋ฅ๊ณผ์ ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ
- ์ต์๊ฐ ์ ๋ฐ์ดํธ
๊ทธ๋ฐ๋ฐ,, ๊ณ์ ํต๊ณผ๊ฐ ์๋ผ์ ์กฐ๊ธ ๋ ๊ณ ๋ฏผํด๋ด์ผํ ๊ฒ ๊ฐ์์ ;; ๐
๋ค์ ๋ฌธ์ ๋์ ํด๋ณด๊ฒ ์ต๋๋ค-!-!!
|
||
island_id += 1 | ||
|
||
min_cost = int(1e6) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ต์๊ฐ์ ์ด๊ธฐ๊ฐ์ ๋ค๋ฅธ ๋ ํผ๋ฐ์ค์๋ sys ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ maxsize๋ฅผ ์ฌ์ฉํ๊ธฐ๋ ํ๋๋ผ๊ณ ์
์ด๋ฏธ ์๊ณ ๊ณ์ค ์๋ ์์ง๋ง, ์ด๊ธฐ๊ฐ์ ๋ญ๋ก ๋ฌ์ผํ ์ง ๊ณ ๋ฏผ์ด์ค ๋๋ sys.maxsize๋ก ๋๋ ๊ฒ๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์์!
๐ ๋ฌธ์ ๋งํฌ
๋ค๋ฆฌ ๋ง๋ค๊ธฐ
โ๏ธ ์์๋ ์๊ฐ
1์๊ฐ
โจ ์๋ ์ฝ๋
bridges_queue
: ๋ค๋ฆฌ๋ฅผ ๋์ ์ง์ ์ ๋ด๋ ํ1. ์ฌ ๊ตฌ๋ถ, ํด์๊ฐ ํ์ : ๋งต ์ ์ฒด๋ฅผ ํ์ธํ๋ฉด์, ๊ฐ ์ฌ์ ID๋ฅผ ํ ๋นํ๊ณ ํด์๊ฐ๋ฅผ ํ์ ์ถ๊ฐ
bridges_queue
์ ์ถ๊ฐํ๋ค :(์ขํ, ๋ค๋ฆฌ๊ธธ์ด 1, ์ฌ ID)
ํ์์ ์์๋ฅผ ์ฌ์ฉ2. ๋ค๋ฆฌ ๋์๋ณด๊ธฐ :
bridges_queue
๋ฅผ ์ํํ๋ฉด์, ๋ค๋ฆฌ์ ์ต์ ๊ธธ์ด๋ฅผ ๊ตฌํ๋คmin_cost
: ๋ค๋ฆฌ ์ต์ ๊ธธ์ดmin_cost
๋ฅผ ๊ฐฑ์ ํ๋คmin_cost = min(min_cost, cost + costs[nr][nc])
ํ ์คํธ ์ผ์ด์ค์ ๋ํด์ ์ต์ ๊ธธ์ด๋ฅผ ๊ตฌํ๋ฉด
3
์ด ๋์จ๋ค์ค๋ฅธ์ชฝ ์ด๋ฏธ์ง์์ ๋ณ ํ์๊ฐ ์๋ ์ง์ ์์ ๊ธธ์ด๊ฐ
3
์ธ ์๋ก ๋ค๋ฅธ ๋ ์ฌ์ ์๋ ๋ค๋ฆฌ๊ฐ ์ด์ด์ง๋ค3.
min_cost
์ถ๋ ฅ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ