File tree Expand file tree Collapse file tree 4 files changed +119
-0
lines changed
Expand file tree Collapse file tree 4 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+ from collections import deque , defaultdict
3+
4+ read = lambda : sys .stdin .readline ().rstrip ()
5+
6+
7+ class Problem :
8+ def __init__ (self ):
9+ self .n , self .m = map (int , read ().split ())
10+
11+ self .moves = defaultdict (int )
12+ for start , end in [map (int , read ().split ()) for _ in range (self .n + self .m )]:
13+ self .moves [start ] = end
14+
15+ def solve (self ) -> None :
16+ print (self .bfs ())
17+
18+ def bfs (self ) -> int :
19+ queue , board = deque ([1 ]), [0 ] * 101
20+
21+ while queue :
22+ pos = queue .popleft ()
23+ if pos == 100 :
24+ return board [pos ]
25+
26+ for dice in range (1 , 7 ):
27+ next_pos = pos + dice
28+ if next_pos > 100 :
29+ continue
30+
31+ if next_pos in self .moves :
32+ next_pos = self .moves [next_pos ]
33+
34+ if board [next_pos ] == 0 :
35+ board [next_pos ] = board [pos ] + 1
36+ queue .append (next_pos )
37+
38+ return - 1
39+
40+
41+ if __name__ == "__main__" :
42+ Problem ().solve ()
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "input" : [
4+ " 3 7" ,
5+ " 32 62" ,
6+ " 42 68" ,
7+ " 12 98" ,
8+ " 95 13" ,
9+ " 97 25" ,
10+ " 93 37" ,
11+ " 79 27" ,
12+ " 75 19" ,
13+ " 49 47" ,
14+ " 67 17"
15+ ],
16+ "expected" : [
17+ " 3"
18+ ]
19+ },
20+ {
21+ "input" : [
22+ " 4 9" ,
23+ " 8 52" ,
24+ " 6 80" ,
25+ " 26 42" ,
26+ " 2 72" ,
27+ " 51 19" ,
28+ " 39 11" ,
29+ " 37 29" ,
30+ " 81 3" ,
31+ " 59 5" ,
32+ " 79 23" ,
33+ " 53 7" ,
34+ " 43 33" ,
35+ " 77 21"
36+ ],
37+ "expected" : [
38+ " 5"
39+ ]
40+ }
41+ ]
Original file line number Diff line number Diff line change 1+ import json
2+ import os .path
3+ import unittest
4+ from io import StringIO
5+ from unittest .mock import patch
6+
7+ from parameterized import parameterized
8+
9+ from main import Problem
10+
11+
12+ def load_sample (filename : str ):
13+ path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), filename )
14+
15+ with open (path , "r" ) as file :
16+ return [(case ["input" ], case ["expected" ]) for case in json .load (file )]
17+
18+
19+ class TestCase (unittest .TestCase ):
20+ @parameterized .expand (load_sample ("sample.json" ))
21+ def test_case (self , case : str , expected : list [str ]):
22+ # When
23+ with (
24+ patch ("sys.stdin.readline" , side_effect = case ),
25+ patch ("sys.stdout" , new_callable = StringIO ) as output ,
26+ ):
27+ Problem ().solve ()
28+
29+ result = output .getvalue ().rstrip ()
30+
31+ # Then
32+ self .assertEqual ("\n " .join (expected ), result )
33+
34+
35+ if __name__ == "__main__" :
36+ unittest .main ()
You can’t perform that action at this time.
0 commit comments