-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀merge: [Week21] BOJ Shortest Path - 2문제 완료 + 1문제 X (#78)
* ✨feat: [BOJ] 01_18352_특정거리의도시찾기 * ✨feat: [BOJ] 02_11403_경로 찾기 * ✨feat: [BOJ] 03_13549_ 숨바꼭질3
- Loading branch information
1 parent
fdfd098
commit 3f7a9e0
Showing
3 changed files
with
88 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,27 @@ | ||
import sys | ||
|
||
n, m, k, x = map(int, sys.stdin.readline().split()) | ||
|
||
city = [set() for _ in range(n + 1)] | ||
for _ in range(m): | ||
a, b = map(int, input().split()) | ||
city[a].add(b) | ||
|
||
result = {x} | ||
beforeCity = {x} | ||
for _ in range(k): | ||
temp = result | ||
result = set() | ||
for nextCity in temp: | ||
if len(city[nextCity]) > 0: | ||
beforeCity.update([nextCity]) | ||
result.update(city[nextCity]) | ||
result -= beforeCity | ||
beforeCity.update(result) | ||
|
||
if len(result) == 0: | ||
print(-1) | ||
else: | ||
sortedResult = sorted(result) | ||
for cityNum in sortedResult: | ||
print(cityNum) |
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,24 @@ | ||
import sys | ||
|
||
|
||
def dfs(graph, start, visited): | ||
for i in range(len(graph[start])): | ||
if graph[start][i] == 1 and not visited[i]: | ||
visited[i] = 1 | ||
dfs(graph, i, visited) | ||
|
||
|
||
N = int(sys.stdin.readline()) | ||
G = [] | ||
for _ in range(N): | ||
row = list(map(int, sys.stdin.readline().split())) | ||
G.append(row) | ||
|
||
result = [[0] * N for _ in range(N)] | ||
for i in range(N): | ||
V = [0] * N | ||
dfs(G, i, V) | ||
result[i] = V | ||
|
||
for x in result: | ||
print(' '.join(str(y) for y in x)) |
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,37 @@ | ||
import sys | ||
sys.setrecursionlimit(10**n) | ||
|
||
def oneSecondCheck(n): | ||
# return [n - 1, n + 1, (n - 1) * 2, n * 2 - 1, n * 2 + 1, (n + 1) * 2] | ||
return [(n + 1) * 2, n * 2 + 1, n * 2 - 1, (n - 1) * 2, n + 1, n - 1] | ||
|
||
|
||
def dfs(n, time): | ||
global K | ||
global halfK | ||
global flag | ||
oneSecondList = oneSecondCheck(n) | ||
if K in oneSecondList: | ||
timeList.append(time) | ||
flag = 1 | ||
for location in oneSecondList: | ||
if location <= halfK and flag == 0: | ||
dfs(location, time + 1) | ||
|
||
|
||
N, K = map(int, sys.stdin.readline().split()) | ||
|
||
# 4, 6, 8, 9, 11, 12 | ||
|
||
if N >= K: | ||
print(N - K) | ||
if N * 2 == K: | ||
print(0) | ||
else: | ||
halfK = K // 2 | ||
if K % 2 == 1: | ||
halfK = (K + 1) // 2 | ||
flag = 0 | ||
timeList = [] | ||
dfs(N, 1) | ||
print(timeList[0]) |