-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-D-Frogger-Hard.py
52 lines (48 loc) · 1.81 KB
/
1-D-Frogger-Hard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
class Frogger():
def __init__(self, n, squares):
self.n = n
self.squares = squares
self.visited = [False for i in range(n)]
self.track_magic_nums = dict([i, {}] for i in range(n))
def toposort(self):
order = [[i, 0] for i in range(self.n)]
for i in range(self.n):
next = i + self.squares[i]
if not(next < 0 or next >= self.n):
order[next][1] += 1
order.sort(key = lambda x: x[1])
return list(map(lambda x: x[0], order))
def count_winning_instances(self):
count = 0
order = self.toposort()
for i in order:
if self.visited[i]:
continue
cur_search = [i]
cur_visited = {}
self.visited[i] = True
count += 1
self.track_magic_nums[i][self.squares[i]] = True
counter = i
cycle = False
while True:
counter += self.squares[counter]
if counter < 0 or cycle or counter >= self.n or cur_visited.get(counter, False):
break
cur_visited[counter] = True
if not self.visited[counter]:
cur_search.append(counter)
self.visited[counter] = True
for j in cur_search:
if not self.track_magic_nums[j].get(self.squares[counter], False):
count += 1
self.track_magic_nums[j][self.squares[counter]] = True
if counter == i:
cycle = True
return count
if __name__ == "__main__":
input = sys.stdin.readline
n = list(map(int, input().split()))[0]
frogger = Frogger(n, list(map(int, input().split())))
print(frogger.count_winning_instances())