-
Notifications
You must be signed in to change notification settings - Fork 0
/
935_np.py
26 lines (25 loc) · 868 Bytes
/
935_np.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
import numpy as np
class Solution(object):
def knightDialer(self, N):
"""
:type N: int
:rtype: int
"""
trans = np.array([
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0]
], dtype=np.clongfloat)
trans_pow = np.linalg.matrix_power(trans, N-1)
res = np.ones(10).dot(trans_pow)
print int(np.sum(res))
return int(np.sum(res) % (10**9 + 7))
solu = Solution()
print solu.knightDialer(161)