Skip to content

Commit

Permalink
Merge pull request #182 from AlgoLeadMe/52-tgyuuAn
Browse files Browse the repository at this point in the history
52-tgyuuAn
  • Loading branch information
tgyuuAn authored May 10, 2024
2 parents b6c36a5 + 69f06c8 commit ed97c91
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 @@ -53,4 +53,5 @@
| 49μ°¨μ‹œ | 2023.03.29 | DP | <a href="https://www.acmicpc.net/problem/10217">KCM Travel</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/174
| 50μ°¨μ‹œ | 2024.04.01 | BFS | <a href="https://www.acmicpc.net/problem/9328">μ—΄μ‡ </a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175
| 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
---
44 changes: 44 additions & 0 deletions tgyuuAn/μœ„μƒμ •λ ¬/κ²Œμž„ 개발.py
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 ed97c91

Please sign in to comment.