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

52-tgyuuAn #182

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions tgyuuAn/BFS/๊ณผ์™ธ๋งจ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import sys
from collections import deque

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

N = int(input())

board = []
idx = 1
for row in range(N):
row_tiles = []
# ํ™€์ˆ˜ ์ค„ ์ผ ๊ฒฝ์šฐ N๊ฐœ๊ฐ€ ๊ทธ๋Œ€๋กœ ๋“ค์–ด์˜ด
if row%2==0:
for col in range(N):
temp = list(input().split())
temp.append(idx)
tile = tuple(temp)

row_tiles.append(tile)
idx += 1

# ์ง์ˆ˜ ์ค„ ์ผ ๊ฒฝ์šฐ N-1๊ฐœ๊ฐ€ ๋“ค์–ด์˜ด
else:
for col in range(N-1):
temp = list(input().split())
temp.append(idx)
tile = tuple(temp)

row_tiles.append(tile)
idx += 1

board.append(row_tiles)

parent_tile = [-1 for _ in range(idx)]

odd_dx = [-1, -1, 0, 0, 1, -1]
odd_dy = [0, 1, -1, 1, 0, 1]

even_dx = [0, 1, -1, 1, 0, 1]
even_dy = [-1, -1, 0, 0, 1, 1]

now = (1, (0, 0), 1) # ํƒ€์ผ ๋ฒˆํ˜ธ, ํ˜„์žฌ ํƒ€์ผ ์ขŒํ‘œ, ํ˜„์žฌ ์ด๋™ํ•œ ํšŸ์ˆ˜
visited = set()
deq = deque([now])
biggest_number_tile = -1
biggest_number_count = int(1e9)
while deq:
now_tile, tile_pos, now_count = deq.popleft()
now_x = tile_pos[0]
now_y = tile_pos[1]

if now_tile > biggest_number_tile:
biggest_number_tile = now_tile
biggest_number_count = now_count

if now_tile == idx-1:
print(now_count)
ans_que = deque([])
now = now_tile
while now != 1:
ans_que.appendleft(now)
now = parent_tile[now]
ans_que.appendleft(1)
print(*ans_que)
break

# ํ™€์ˆ˜๋ฒˆ ์งธ ์ค„ ์ผ ๊ฒฝ์šฐ,
if now_y%2 == 0:

for dir in range(6):
new_x = now_x + odd_dx[dir]
new_y = now_y + odd_dy[dir]

if new_x < 0 or new_x >= N: continue
if new_y%2 == 1 and new_x >= N-1: continue
if new_y < 0 or new_y >= N: continue

new_tile = board[new_y][new_x]
new_tile_idx = new_tile[-1]

if new_tile_idx in visited: continue
if dir in (0,1,5) and board[now_y][now_x][0] != board[new_y][new_x][1]: continue
if dir in (2,3,4) and board[now_y][now_x][1] != board[new_y][new_x][0]: continue

visited.add(new_tile_idx)
parent_tile[new_tile_idx] = now_tile
deq.append((new_tile_idx, (new_x, new_y), now_count+1))

# ์ง์ˆ˜๋ฒˆ ์งธ ์ค„ ์ผ ๊ฒฝ์šฐ,
else:
for dir in range(6):
new_x = now_x + even_dx[dir]
new_y = now_y + even_dy[dir]

if new_x < 0 or new_x >= N: continue
if new_y%2 == 1 and new_x >= N-1: continue
if new_y < 0 or new_y >= N: continue

new_tile = board[new_y][new_x]
new_tile_idx = new_tile[-1]

if new_tile_idx in visited: continue
if dir in (0,2,4) and board[now_y][now_x][0] != board[new_y][new_x][1]: continue
if dir in (1,3,5) and board[now_y][now_x][1] != board[new_y][new_x][0]: continue

visited.add(new_tile_idx)
parent_tile[new_tile_idx] = now_tile
deq.append((new_tile_idx, (new_x, new_y), now_count+1))

else:
print(biggest_number_count)
ans_que = deque([])
now = biggest_number_tile
while now != 1:
ans_que.appendleft(now)
now = parent_tile[now]
ans_que.appendleft(1)
print(*ans_que)

# ํ™€์ˆ˜๋ฒˆ ์งธ ์ค„์—์„œ ์ด๋™ํ•  ๋•Œ,
# ์ง์ˆ˜๋ฒˆ ์งธ ์ค„๋กœ ํ–‰ ์ด๋™์„ ํ•  ๋•Œ์—๋Š” N๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ฟ ์•„๋‹ˆ๋ผ N-1๋ฒˆ์งธ ์ธ๋ฑ์Šค๋กœ๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅ.
# ๋‹น์—ฐํ•˜๊ฒ ์ง€๋งŒ ์—ด์˜ 0๋ฒˆ ์ธ๋ฑ์Šค์—์„œ๋Š” 0๋ฒˆ์œผ๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Œ.
# ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—ด์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์—์„œ๋Š” N-1๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Œ.

# ์ง์ˆ˜๋ฒˆ ์งธ ์ค„์—์„œ ํ™€์ˆ˜๋ฒˆ ์งธ ์ค„์œผ๋กœ ํ–‰ ์ด๋™ํ•  ๋•Œ์—๋Š”,
# N๋ฒˆ์งธ ์ค„ ๋ฟ์•„๋‹ˆ๋ผ, N+1๋ฒˆ์งธ ์ค„๋กœ๋„ ์ด๋™ ๊ฐ€๋Šฅ.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@
| 44์ฐจ์‹œ | 2023.03.13 | ํŠธ๋ผ์ด | <a href="https://www.acmicpc.net/problem/14725">๊ฐœ๋ฏธ๊ตด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159
| 45์ฐจ์‹œ | 2023.03.16 | ํŠธ๋ผ์ด | <a href="https://www.acmicpc.net/problem/31413">ํŠธ๋ผ์ด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162
| 51์ฐจ์‹œ | 2023.04.07 | BFS | <a href="https://www.acmicpc.net/problem/5213">๊ณผ์™ธ๋งจ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/179
| 52์ฐจ์‹œ | 2023.05.06 | ์œ„์ƒ์ •๋ ฌ | <a href="https://www.acmicpc.net/problem/1516">๊ฒŒ์ž„ ๊ฐœ๋ฐœ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/182
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys

def input(): return sys.stdin.readline()
INF = int(1e9)

visited = set()
N = int(input())
indegree = [[0,set()] for _ in range(N+1)]
receipe = [[] for _ in range(N+1)]
outdegree = [[] for _ in range(N+1)]
answer = [INF for _ in range(N+1)]
can_build = []

for idx in range(1,N+1):
_input = list(map(int,input().split()))
indegree[idx][0] = _input[0]
receipe[idx] = _input[1:-1]
indegree[idx][1] = set(_input[1:-1])
for out in _input[1:-1]:
outdegree[out].append(idx)

if len(indegree[idx][1]) == 0:
can_build.append(idx)
visited.add(idx)
answer[idx] = _input[0]

while can_build:
now = can_build.pop()

for next_building in outdegree[now]:
indegree[next_building][1].discard(now)
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ €๋Š” ์ด์ „์— ์ง€์–ด์ ธ์•ผ ํ•  ๊ฑด๋ฌผ์„ ๊ทธ๋ƒฅ list๋กœ ์ €์žฅํ•ด์„œ ์ผ๋Š”๋ฐ,, set์œผ๋กœ ์ €์žฅํ•ด๋‘๋ฉด discard๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๊ตฐ์šฉ ..
discard ์ฒ˜์Œ ๋ณด๋„ค์š” ๊ฐ’์˜ ์กด์žฌํ•จ์ด ๋ณด์žฅ๋˜๋ฉด remove(), ๋ณด์žฅ๋˜์ง€ ์•Š์œผ๋ฉด discard() ์ค์ค ํ•ด๊ฐ‘๋‹ˆ๋‹ค!!

Copy link
Member Author

Choose a reason for hiding this comment

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

@H0ngJu

๊ทผ๋ฐ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์•ž์— ์ง€์–ด์ ธ์•ผ ํ•  ๊ฑด๋ฌผ ๋ฒˆํ˜ธ๋ฅผ ๊ตณ์ด ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ํ™์ฅฌ๋ฅด๊ฐ€ ํ‘ผ ๋ฐฉ์‹๋Œ€๋กœ indegree๋กœ๋งŒ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์ข‹์•„๋ณด์ด๋Š”๋ฐ์š”?

์–ด์ฐจํ”ผ ์œ„์ƒ์ •๋ ฌ์ด๋‹ˆ๊นŒ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๊ฑด์„ค๋ ํ…Œ๋‹ˆ๊นŒ................................ ๊ต‰์žฅํ•ด ์—„์ฒญ๋‚˜...............

0์ผ๋•Œ๋งŒ heap์— ๋„ฃ๊ฒŒ ํ•ด์„œ ์ด๋ฏธ ์ง€์–ด์ง„ ๊ฑด๋ฌผ๋“ค์€ ์Œ์ˆ˜๋กœ ๊ฐ€๋ฒ„๋ฆฌ๋‹ˆ๊นŒ... ์™€์šฐ


if len(indegree[next_building][1]) == 0 and next_building not in visited:
can_build.append(next_building)

max_temp = -1
for ingredient in receipe[next_building]:
max_temp = max(max_temp, answer[ingredient])

answer[next_building] = min(answer[next_building], max_temp + indegree[next_building][0])
visited.add(next_building)

for ans in answer[1:]:
print(ans)