-
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.
- 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) |