-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
16-H0ngJu #191
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
๋ฑ ๋๋ ์ฌ๋ค๋ฆฌ๋ "๋ฐ๋์" ํ์ผํฉ๋๋ค. ๋ํ ๊ฐ ์นธ ๋ณ๋ก ๋ฑ ๋๋ ์ฌ๋ค๋ฆฌ๊ฐ ์ต๋ 1๊ฐ ์กด์ฌํฉ๋๋ค.
๋ฐ๋ผ์ ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ์ ๋ํ ์ ๋ณด๋ฅผ ๋์ ๋๋ฆฌ๋ก ์ ์ฅํด๋๊ณ , ํ์ฌ ์นธ์ ๋ฑ ๋๋ ์ฌ๋ค๋ฆฌ๊ฐ ์๋ค๋ฉด ๊ทธ๊ฑฐ๋ฅผ ํ๊ณ ์๋ ๋ก์ง์ ๋ฌด์ํ๋๋ก ํด๋ฒ๋ฆฌ๋ฉด ์ฝ๋๋ฅผ ๊น๋ํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. -
์ต์ข ์ ์ผ๋ก ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฑด ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฌ๋ ํ์์ ๋๋ค.
์ด๋ ์ผ์ข ์ ์ต๋จ ๊ฒฝ๋ก ๋ฌธ์ ๋ก ๋ณผ ์ ์๋๋ฐ, visited ๋ฆฌ์คํธ์ deque์ ์ฃผ์ฌ์ ๊ตด๋ฆฐ ํ์๋ฅผ ๋ณ๋๋ก ์ ์ฅํ์ง ์๊ณ distances๋ ๋ฆฌ์คํธ ํ๋๋ก ๋ชจ๋ ํด๊ฒฐ ๊ฐ๋ฅํฉ๋๋ค.
from collections import deque
input = open(0).readline
N, M = map(int, input().split())
distances = [float('inf')] * 101 # ๊ฐ ์นธ์ ์ค๊ธฐ๊น์ง ๊ตด๋ฆฐ ์ฃผ์ฌ์ ์ต์ ํ์
ladder_and_snake = {} # ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๋ ๊ฐ ์นธ ๋ณ ์ต๋ 1๊ฐ
for _ in range(N + M):
a, b = map(int, input().split())
ladder_and_snake[a] = b # ๋์
๋๋ฆฌ ํํ๋ก ํ ๋ฒ์ ์ ์ฅ
q = deque([1]) # ์ต์ด ์์์
distances[1] = 0 # ์์์ ์์์ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฐ ํ์๋ 0
while q:
u = q.popleft() # ํ์ฌ ์นธ ์์น
if u == 100: # ๋์ฐฉ์ง๋ฉด stop
break
if u in ladder_and_snake: # ์ฐ์ ํ์ฌ ์นธ์ ๋ฑ ๋๋ ์ฌ๋ค๋ฆฌ๊ฐ ์๋ ์ง ํ์ธ
v = ladder_and_snake[u]
if distances[u] < distances[v]: # ๋ชฉ์ ์ง ์นธ๊น์ง ๊ฐ๋ ๋ฐ ๊ตด๋ฆฐ ์ฃผ์ฌ์ ํ์๊ฐ ๋ ๋ง์ ๊ฒฝ์ฐ์๋ง
distances[v] = distances[u] # ๋ฑ, ์ฌ๋ค๋ฆฌ๋ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฌ์ง ์์ผ๋ฏ๋ก ๊ฐ ๊ทธ๋๋ก ๋ค์ด๊ฐ
q.append(v)
continue # ์๋ค๋ฉด ๋ฐ๋์ ํ์ผํ๋ฏ๋ก ์๋ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ ค๋ณด๋ ๋ก์ง์ ์ ๊ทผํ์ง ์์
for dice in range(1, 7): # ๋ฑ, ์ฌ๋ค๋ฆฌ๊ฐ ์๋๋ฉด ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆผ
v = u + dice # ๋ค์ ์นธ์ ํ์ฌ ์นธ + ์ฃผ์ฌ์ ๋
if v <= 100 and distances[u] + 1 < distances[v]: # 100์ ๋์ง ์์ผ๋ฉด์ ๊ตด๋ฆฐ ํ์๊ฐ ๋ ์ ์ ๊ฒฝ์ฐ์๋ง
distances[v] = distances[u] + 1 # ์ฃผ์ฌ์๋ฅผ ๊ตด๋ ธ์ผ๋ฏ๋ก + 1
q.append(v)
print(distances[100]) # while๋ฌธ์ด ์ข
๋ฃ๋๋ฉด ๋ชฉ์ ์ง ์นธ์ ์ ์ฅ๋ ์ฃผ์ฌ์ ํ์ ์ถ๋ ฅ.
์ฌ๋ฐ๋ ๋ฌธ์ ์๋ค์ :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from collections import deque, defaultdict
import sys
def input(): return sys.stdin.readline().rstrip()
START = 1
TARGET = 100
N, M = map(int,input().split())
ledder = defaultdict(int)
special = set()
for _ in range(N+M):
start, destination = map(int,input().split())
ledder[start] = destination
special.add(start)
deq = deque([(0,START)])
visited = {START,}
while deq:
now_count, now_position = deq.popleft()
if now_position == TARGET:
print(now_count)
break
for dice in range(1,7):
new_position = now_position + dice
if new_position in special: new_position = ledder[new_position]
if new_position in visited: continue
if new_position > 100: continue
deq.append((now_count+1, new_position))
visited.add(new_position)
์ ๋ ํ์ฃผ๋์ด๋ ๋น์ทํ๊ฒ๋ ํ์๋ค๋ง ๋ก์ง์ ๊ฐ์๋ฐ ์กฐ๊ธ์ ์ฝ๋๊ฐ ๋ฌ๋ผ์!!!!
์ฒดํฌํ์๋ฉด ์ข์ ๋ฏ?!!!!
๋๋ฌด๋๋ฌด ๊ณ ์ํ์ จ์๋๋ค (__) ๐๐๐
ladder = [list(map(int, input().split())) for _ in range(N)] | ||
snake = [list(map(int, input().split())) for _ in range(M)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ ๊ฐ๋ฅผ ๋ถ๋ฆฌํ๋ ๊ฒ๋ ๊ด์ฐฎ์๋ฐ ์ ๊ฐ ๋ดค์ ๋ ๋ ๊ฐ์ ์ฑ๊ฒฉ์ด ๋น์ทํ ๊ฒ ๊ฐ์์ ๊ตณ์ด ๋๋ ํ์ ์์ ๊ฒ ๊ฐ์์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ํ๋๋ก ํ์ต๋๋ฅ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ์๊ฐํด๋ณด๋๊น ๊ทธ๋ฌ๋ค์
์ด์ฐจํผ a,b๋ฅผ ๋ฐ์์ ๋ a>b์ด๋ฉด ๋ฑ์ด๊ณ a<b๋ฉด ์ฌ๋ค๋ฆฌ๋๊น ..
๋ฑ ์ฌ๋ค๋ฆฌ ํด์ ๊ทธ๋ฅ ๊ตฌ๋ถ์ง์ด๋ฒ๋ ธ๋ค์ ..
์๊ฐ์น๋ ๋ชปํ๋ ๋ถ๋ถ์ธ๋ฐ ์ฝ๋ฉํธ ๊ฐ์ฌํฉ๋๋ท ~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ตฌํ์ ์์ด๋ ค์ ๋๋ฐ bfs ์์ ๋ ์ธ๋ฆฌ๋๊ฒ ์ ค ์ด๋ ค์ ๋ค์.. ์จ๋ฐ๊ผญ์ง ๋ฌธ์ ๋ ๋น์ทํ ๊ฒ ๊ฐ์์!!
import java.io.BufferedReader
import java.io.InputStreamReader
const val MAX = 987_654_321
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val (n, m) = br.getInputs()
val arr = Array(101) { MAX }
val q = ArrayDeque<Int>()
val letterOrSnake = mutableMapOf<Int, Int>()
repeat(n) {
val (x, y) = br.getInputs()
letterOrSnake[x] = y
}
repeat(m) {
val (u, v) = br.getInputs()
letterOrSnake[u] = v
}
arr[1] = 0
q.add(1)
while (q.isNotEmpty()) {
val now = q.removeFirst()
val nextOrNull = letterOrSnake[now]
if (nextOrNull != null) {
if (arr[nextOrNull] > arr[now]) {
arr[nextOrNull] = arr[now]
q.add(nextOrNull)
}
continue
}
for (i in 1..6) {
val next = now + i
if (next > 100) {
continue
}
if (arr[next] > arr[now] + 1) {
arr[next] = arr[now] + 1
q.add(next)
}
}
}
println(arr[100])
}
private fun BufferedReader.getInputs() = readLine().split(" ").map { it.toInt() }
ladder = [list(map(int, input().split())) for _ in range(N)] | ||
snake = [list(map(int, input().split())) for _ in range(M)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ํ๋๋ก ํ์ต๋๋ฅ
# ์ฌ๋ค๋ฆฌ ๊ฐ ์ ์๋์ง ๊ฒ์ฌ | ||
for l, n in ladder: | ||
if cur + i == l: | ||
if visited[n-1] == 0: | ||
q.append((n, dice+1)) | ||
visited[n-1] = 1 | ||
check = 1 | ||
break | ||
|
||
if check: continue | ||
# ๋ฑ ์๋์ง ๊ฒ์ฌ | ||
for s, n in snake: | ||
if cur + i == s: | ||
if visited[n-1] == 0: | ||
q.append((n, dice+1)) | ||
visited[n-1] = 1 | ||
check = 1 | ||
break | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ๋๋ก ํฉ์น ์ ์๊ฒ ๋ค์ฉ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ทธ๋ฌ๋ค์ ์ด์ check๋ ํ์ ์๋ค์ .. ๐จ๐จ
์ฌ๋ค๋ฆฌ๊ฐ ladder ์๊ตฐ์....? |
์คํ .. ๋์
๋๋ฆฌ๋ก ์ ์ฅํ๊ณ ๊ตณ์ด visited๋ก ๋ฐ๋ก ๋ฐฉ๋ฌธ ๊ด๋ฆฌ๋ฅผ ํ์ง ์๋๋ค..!! ๋ด์ ์จ๋จน๊ฒ ์ต๋๋ค ๐ |
ladder = [list(map(int, input().split())) for _ in range(N)] | ||
snake = [list(map(int, input().split())) for _ in range(M)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด์งํผ ๋ฐ์๋ start end๋ ๊ฐ๋๋ผ๊ตฌ์!!
์ฌ๋ค๋ฆฌ ๋จผ์ ๋ฐ์ง๋ง ์ฌ๋ค๋ฆฌ๋ ์๋ก ์ฌ๋ผ๊ฐ๋๋ฐ
start -> end๋ก start < end๊ฐ ๋๊ณ
๋ฑ์ ์๋๋ก ๋ด๋ ค๊ฐ์ง๋ง input์ ๋์ผํ๊ฒ
start -> end๋ก start > end๊ฐ ๋๋ ๋ค๋ฅธ์ ๋ง ์์๋ฟ ๊ฐ๋๋ผ๊ตฌ์!
import sys | ||
from collections import deque | ||
|
||
def input(): return sys.stdin.readline().rstrip() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
input ๋ฐ๊พธ์ค๋ ์ด๋ ๊ฒ ์ฐ์๋๊ตฐ์!!
์ ๋
input = sys.stdin.readline
์ผ๋ก ๋ฐ๊ฟ์ ์ ๊ธฐํ๋ค์!!
๐ ๋ฌธ์ ๋งํฌ
๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ๊ฒ์
โ๏ธ ์์๋ ์๊ฐ
40M
โจ ์๋ ์ฝ๋
์ด๋ฒ ๋ฌธ์ ๋ ๊ธ๋ฐฉ ๋ก์ง์ ์งฐ์ต๋๋ค ํํ
๋ฌธ์ ์์ฝ
๋ฑ-์ฌ๋ค๋ฆฌ ๊ฒ์์์ ์ฃผ์ฌ์๋ฅผ ์กฐ์ํด์ 100๊น์ง ๋๋ฌํ์@! ์ด๋ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฌ๋ ์ต์ ํ์๋?
์๊ณ ๋ฆฌ์ฆ ๋ถ๋ฅ
์ ๋ฌธ์ ๋ณด๊ณ ๋ฐ๋ก ์ค ์ด๊ฑฐ BFS๋ค~๋ก ์๊ฐํด์ ๋ฑ๋ค๊ณ ๋ฐ๋ก ์ ๊ทผํ์ต๋๋ค ๐ฅ
๊ทธ๋ฅ ํํ BFS ๋ฌธ์ ์ ๋น์ทํด์ ๋ก์ง์ ์ง๋ ๋ฐ ํฌ๊ฒ ์ฃผ์ํด์ผํ ๋ถ๋ถ์ ์์์ต๋๋ค
๋ค๋ง, ์ ๋ก์ง์์๋
์ฌ๋ค๋ฆฌ๊ฐ ์๋์ง, ๋ฑ์ด ์๋์ง, ๊ทธ๋ฅ ์ผ๋ฐ ์นธ์ธ์ง ๊ตฌ๋ถํ๊ธฐ ์ํด
check
์ด๋ผ๋ flag๋ฅผ ๋ฐ๋ก ๋์์ต๋๋ค.์๋ฅผ ๋ค์ด,
ํ์ฌ ์นธ์ 40, ์ฃผ์ฌ์๋ 3์ด๊ณ , 43๋ฒ์งธ ์นธ์ด
์ฌ๋ค๋ฆฌ
์นธ์ด๋ผ๋ฉด,๋ฑ
,์ผ๋ฐ
์นธ ๋ก์ง์ ํ์ธํ ํ์๊ฐ ์์ผ๋ฏ๋กcheck = 1๋ก ๋ฐ๊พธ์ด ์ฃผ๊ณ , ๋ค์ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฌ๋ฉด ๋ฉ๋๋ค.
๋ค์ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฌ๊ฒ๋๋ฉด ์ฃผ์ฌ์๊ฐ 4๊ฐ ๋๋ฏ๋ก, 44๋ฒ์งธ ์นธ์ ๋ํ ๋ก์ง์ ์งํํ๋ฉด ๋ฉ๋๋ค
์ต์ข ์๋ ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ