-
Notifications
You must be signed in to change notification settings - Fork 1
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
20-alstjr7437 #71
20-alstjr7437 #71
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.
ํ ๋ค์ด๋ฒ ์ฝํ
์ง์ ์ ํ์๋ ๋ฌธ์ ๋ผ ๋ค์ ํ์ง๋ ์์์ต๋๋ค..ํํ
์ ๋ bfs ๋ก ํ์ํ๊ณ ์ฐพ์ผ๋ฉด return ํ๋๋ก ํ์๋๋ฐ ํธ๋ฆฌ๋ก ํธ์
จ๊ตฐ์?
์๊ฐ์ง ๋ชปํ ๊ตฌํ ๋ฐฉ๋ฒ ๊ฐ์ฌํฉ๋๋ค..!
val max = 100_001
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val visited = MutableList(max) { 0 }
val queue = ArrayDeque<Int>()
val (x, y) = br.readLine().split(" ").map { it.toInt() }
queue.add(x)
visited[x] = 1
while (queue.isNotEmpty()) {
val now = queue.removeFirst()
if (now == y) {
println(visited[now]-1)
return
}
val nows = mutableListOf<Int>()
nows.add(now + 1)
nows.add(now - 1)
nows.add(2 * now)
for (i in nows.indices) {
if (nows[i] > max - 1 || nows[i] < 0) {
continue
}
if (visited[nows[i]] == 0) {
visited[nows[i]] = visited[now] + 1
queue.add(nows[i])
}
}
}
}
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.
์ฒ์์ ์ด ์ ๊ตณ์ด result ๋ฅผ ๋ฐ๋ก ๋นผ์ ์ด๋ ์๊ฐ์ ์ ์ฅํ์ง ํ๊ณ ๊ทธ๋ฅ ์์ q์ ์ ์ฅํ๋๋ฐ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋๋ค์..!
# ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ์ฝ๋
import sys
input=sys.stdin.readline
from collections import deque
N,K=map(int, input().split())
q=deque()
q.append((N,0))
result = 0
while q:
current,second = q.popleft()
if current==K:
result = second
break
q.append((current-1, second+1))
q.append((current+1, second+1))
q.append((current*2, second+1))
print(result)
์๊ณ ๋ณด๋ ๋ฉ๋ชจ๋ฆฌ ์ ํ์ด 128MB ์๊ณ ์ด๋ง๋ฌด์ํ ๊ฒฝ์ฐ์ ์๊ฐ ๋์ฌ ์ ์๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋๊ฒ ๊ฑฐ๋ ํ๊ณ ์์ํ ๋ฏผ์ ๋์ฒ๋ผ ํ์์ด๋ค..
import sys
input=sys.stdin.readline
from collections import deque
N,K=map(int, input().split())
q=deque()
q.append(N)
result = [0] * 100001
while q:
current = q.popleft()
if current==K:
break
if current-1 >=0 and result[current-1] ==0:
result[current-1] = result[current] + 1
q.append(current-1)
if current+1 <=100000 and result[current+1] ==0:
result[current+1] = result[current] + 1
q.append(current+1)
if current*2 <=100000 and result[current*2] ==0:
result[current*2] = result[current] + 1
q.append(current*2)
print(result[K])
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๋ก ํ์์ต๋๋ค! ์ํฌ๊ฐ 0~100,000 ์ฌ์ด์ ์์ ์ ์์ด์
Dist ๋ฆฌ์คํธ์ ํฌ๊ธฐ๋ฅผ 10**6์ผ๋ก ์ด๊ธฐํํด์ฃผ๊ณ ์งํํ์ต๋๋ค.
from collections import deque
import sys
input = sys.stdin.readline
INF=10**6
def bfs():
q = deque()
q.append(N)
while q:
x = q.popleft()
if x == M:
print(dist[x])
break
for nx in (x-1, x+1, x*2):
if 0 <= nx < INF and dist[nx] == 0:
dist[nx] = dist[x] + 1
q.append(nx)
N, M = map(int,input().split())
dist = [0] * (INF)
bfs()
๐ ๋ฌธ์ ๋งํฌ
์จ๋ฐ๊ผญ์ง
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
๐ ๋ฌธ์ ์ค๋ช
์์ ) n -> 5 k -> 17
n : 5 -> 10 -> 9 -> 18 -> 17
โจ ์๋ ์ฝ๋
์ค๋๋ง์ ๋ฐฑ์ค ์๋์ ์๋ ๋ฌธ์ ์ ํ์ ์๋ณด๊ณ ํ์๋๋ฐ
์ฒ์์ ์ฌ์ค ์์นจ ์๊ณ ๋ฆฌ์ฆ ์์ ๋ค์ผ๋ฉฐ ๋ฌธ์ ์ ์ ํ๋ฉด์ ์ค์ DP ๋ฌธ์ ์๋๊ฐ ์๊ฐํด๋๊ณ
๋ค๋ฅธ ๊ณต๋ถ๋ฅผ ํ๋ฉด์ ํ์ด ๋ ๋(๊ธธ ๊ฑธ์๋ ๋ฑ๋ฑ..) ์๊ฐํด๋ณด๋๊น ํธ๋ฆฌ๋ฅผ ๋ง๋ค๋ฉด ๋๋ ๋ฌธ์ ์์ต๋๋ค!!
์ผ๋จ ์ฌ์ฌํ ๋ ๋์ ์ธ๊ฑธ๋ก๋ ์๋ ์ฌ์ง๊ณผ ๊ฐ์ด
-1, +1, *2๋ก 3๊ฐ๋๋ก ๋๋ ์ง๋ฉด์
๋ง์ฝ์ k๋ผ๋ ๋ถ๋ถ์ ์ค๊ฒ ๋๋ฉด
ํด๋น ๋ถ๋ถ์ Level์ ์ถ๋ ฅํ๋ฉด ๋๋ ๋ฌธ์ ์๊ตฌ๋ ํ๋ฉด์ ํ์์ต๋๋ค!!
๊ทธ๋ฆฌ๊ณ ๋ฌดํ ๋ฃจํ ๋ฐ ์๊ฐ์ ์ค์ด๊ธฐ ์ํด ์ค๋ณต์ผ๋ก ๋ฐฉ๋ฌธํ๋ ์ซ์๋ Level๋ก visited ์ฒ๋ฆฌ ํด์คฌ์ต๋๋ค.
๐ป ์ต์ข ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
๋ฌธ์ ๋ฅผ ๋ณด๊ณ ์ด์ ๊ทธ๋ํ๋ฅผ ํตํด ๊ตฌํํด์ผ ํ๋์ง ์กฐ๊ธ์ฉ ๊ฐ์ ์ก์๊ฐ๊ณ ์๋ ๊ฒ ๊ฐ๋ค!!!