-
Notifications
You must be signed in to change notification settings - Fork 0
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 #204 from wellFoundedDevelopers/jimin/50week
[이지민] - 북쪽나라의 도로, 숫자고르기, 연구소2
- Loading branch information
Showing
3 changed files
with
146 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,40 @@ | ||
''' | ||
https://dodobow.tistory.com/35 | ||
근데 왜 1과 1로 돌고 난 후의 max만 확인하면 될까? | ||
''' | ||
|
||
import sys | ||
from collections import deque | ||
|
||
roads = [[] for _ in range(10_001)] | ||
while True: | ||
try: | ||
a, b, c = map(int, sys.stdin.readline().split()) | ||
roads[a].append([b, c]) | ||
roads[b].append([a, c]) | ||
except: | ||
break | ||
|
||
|
||
def bfs(start): | ||
queue = deque([start]) | ||
|
||
visited[start] = 0 | ||
|
||
while queue: | ||
now = queue.popleft() | ||
|
||
for i, w in roads[now]: | ||
if visited[i] == -1: | ||
visited[i] = visited[now] + w | ||
queue.append(i) | ||
|
||
|
||
visited = [-1 for _ in range(10_001)] | ||
bfs(1) | ||
s = visited.index(max(visited)) | ||
visited = [-1 for _ in range(10_001)] | ||
bfs(s) | ||
print(max(visited)) |
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,36 @@ | ||
''' | ||
처음에는 그냥 dfs로 했더니 시간초과났다 | ||
사이클을 도는 친구를 찾아야한다.. | ||
https://velog.io/@deannn/BOJ-%EB%B0%B1%EC%A4%80-2668%EB%B2%88-%EC%88%AB%EC%9E%90%EA%B3%A0%EB%A5%B4%EA%B8%B0-Python | ||
참고 | ||
''' | ||
|
||
import sys | ||
n = int(sys.stdin.readline()) | ||
numbers = [0] | ||
for i in range(n): | ||
numbers.append(int(sys.stdin.readline())) | ||
|
||
answer = set() | ||
|
||
|
||
def dfs(first, second, now): | ||
first.add(now) | ||
second.add(numbers[now]) | ||
if numbers[now] in first: | ||
if first == second: | ||
answer.update(first) | ||
return | ||
return | ||
return dfs(first, second, numbers[now]) | ||
|
||
|
||
for i in range(1, n + 1): | ||
if i not in answer: | ||
dfs(set(), set(), i) | ||
|
||
print(len(answer)) | ||
for num in sorted(list(answer)): | ||
print(num) | ||
|
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,70 @@ | ||
import sys | ||
from collections import deque | ||
from itertools import combinations | ||
|
||
n, m = map(int, sys.stdin.readline().split()) | ||
|
||
ground = [] | ||
for i in range(n): | ||
ground.append(list(map(int, sys.stdin.readline().split()))) | ||
|
||
mini = 100_000_000 | ||
|
||
|
||
def bfs(viruses): | ||
global n, mini, ground | ||
queue = deque(viruses) | ||
|
||
visited = [[0 for _ in range(n)] for _ in range(n)] | ||
for i in range(n): | ||
for j in range(n): | ||
if ground[i][j] == 1: | ||
visited[i][j] = -1 | ||
|
||
for x, y in viruses: | ||
visited[x][y] = 1 | ||
|
||
dx = [0, 0, -1, 1] | ||
dy = [1, -1, 0, 0] | ||
|
||
while queue: | ||
nx, ny = queue.popleft() | ||
|
||
for i in range(4): | ||
if 0 <= nx + dx[i] < n and 0 <= ny + dy[i] < n and visited[nx + dx[i]][ny + dy[i]] == 0: | ||
queue.append([nx + dx[i], ny + dy[i]]) | ||
visited[nx + dx[i]][ny + dy[i]] = visited[nx][ny] + 1 | ||
|
||
can = True | ||
for i in range(n): | ||
for j in range(n): | ||
if visited[i][j] == 0: | ||
can = False | ||
break | ||
|
||
if not can: | ||
return | ||
|
||
maxi = 0 | ||
for v in visited: | ||
maxi = max(maxi, max(v)) | ||
|
||
mini = min(maxi - 1, mini) | ||
|
||
|
||
possible = [] | ||
for i in range(n): | ||
for j in range(n): | ||
if ground[i][j] == 2: | ||
possible.append([i, j]) | ||
|
||
|
||
for combi in combinations(possible, m): | ||
bfs(list(combi)) | ||
|
||
if mini == 100_000_000: | ||
print(-1) | ||
else: | ||
print(mini) | ||
|
||
|