-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDP_solution
68 lines (57 loc) · 1.87 KB
/
DP_solution
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy
import time
dp_table = numpy.zeros(shape=(1001,51,5,5))
def initialize():
for j in range(0,51):
for k in range(0,5):
for l in range(0,5):
cur_max=max(3,j)
for i in range(0,cur_max+1):
dp_table[i][j][k][l]=(i,0)
def lookahead(n,curmax, my_reset, op_reset, rnge):
if n <1 :
return True
for i in range(1,rnge+1):
if n-i==0:
return False
if n-i > 0:
if dp_table[n-i][max(curmax,i+1)][my_reset][op_reset] ==(-1,0):
return False
if op_reset>0:
if lookahead(n-i,max(curmax,i+1),op_reset-1,my_reset, rnge)==True:
return False
return True
def playmove():
for j in range(0,51):
for i in range(j+1,1001):
for k in range(0,5):
for l in range(0,5):
if dp_table[i][j][k][l]:
continue
can_win=(1,0)
take = min(i,j)
for p in range(1,take+1):
if p==j and j<50:
if dp_table[i-p][j+1][l][k]==0:
can_win=(p,0)
break
elif dp_table[i-p][j][l][k] ==0:
can_win=(p,0)
break
if k>0: # rather than just looking for 0 look 2 moves ahead for every one of these and look whether all 1's
for n in range(1,take):
if lookahead(i-n,j,k-1,l,3) and dp_table[i-n][j][k-1][l]==0:
can_win=(n,1)
break
if j<50 and j<=i:
if lookahead(i-j,j+1,k-1,l,3) and dp_table[i-j][j+1][k-1][l]==0:
can_win=(j,1)
dp_table[i][j][k][l]=can_win
initialize()
print(dp_table[2][3][1][1])
playmove()
print("**********************************")
print(dp_table[2][3][1][1])
#while(1):
# a,b,c,d=input("Enter indices in the format (5,3,2,1) :")
# dp_table[a][b][c][d]