Skip to content

Commit 3d4f53d

Browse files
committed
백준 1697번 숨바꼭질
문제 링크: https://www.acmicpc.net/problem/1697
1 parent ad9b4c0 commit 3d4f53d

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

백준 1697번 숨바꼭질/main.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import sys
2+
from typing import List
3+
4+
# python3로는 시간초과로 정답 판정이 안되고
5+
# pypy3로 해서 실행시간을 좀 줄여야, 정답 인정이 됩니다.
6+
def solve():
7+
N, K = map(int, sys.stdin.readline().strip().split(' '))
8+
mx = max(N, K)
9+
dt: List[int] = [sys.maxsize for _ in range(mx + 10)]
10+
l = len(dt)
11+
dt[N] = 0
12+
13+
if N == K:
14+
print(0)
15+
return
16+
17+
time = 0
18+
queue = []
19+
add_plus_minus_x2(queue, l, dt, N, time)
20+
21+
while dt[K] == sys.maxsize:
22+
time += 1
23+
queue2 = []
24+
25+
for n in queue:
26+
dt[n] = min(dt[n], time)
27+
28+
if n == K:
29+
print(time)
30+
return
31+
32+
add_plus_minus_x2(queue2, l, dt, n, time)
33+
34+
queue = queue2
35+
36+
print(time)
37+
38+
39+
def add_plus_minus_x2(queue: List[int], l, dt, N, time):
40+
if (N - 1 >= 0) and (N - 1 not in queue) and (dt[N - 1] > time):
41+
queue.append(N - 1)
42+
if (N + 1 < l) and (N + 1 not in queue) and (dt[N + 1] > time):
43+
queue.append(N + 1)
44+
if (N * 2 < l) and (N * 2 not in queue) and (dt[N * 2] > time):
45+
queue.append(N * 2)
46+
47+
48+
if __name__ == '__main__':
49+
solve()

백준 1697번 숨바꼭질/test1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5 17
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')

0 commit comments

Comments
 (0)