-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from AlgoLeadMe/52-tgyuuAn
52-tgyuuAn
- Loading branch information
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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λ²μ§Έ μ€λ‘λ μ΄λ κ°λ₯. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |