Skip to content

Commit

Permalink
2024-05-07
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed May 6, 2024
1 parent 9024ee4 commit 69f06c8
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
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)

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)

0 comments on commit 69f06c8

Please sign in to comment.