Advertisement
Guest User

Cookies - SCTF

a guest
Mar 1st, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. def gendp(x):
  2. values = {} #it maps the ordered pair (numpieces, prevmove) to 0 (you win if you start the turn on this condition)
  3. #and 1 (you win the game if you start the turn on this condition)
  4. for i in range(0,x+1):
  5. values[(0,i)]=0 #if you have 0 pieces and its your turn, you autolose
  6. for i in range(1,50): #m range appears to be 2 through 49
  7. #so, this is an impartial game (it doesnt matter whose turn it is, you can make the same moves)
  8. #Say you're in state A. If you can get to a losing state (value of 0) in one move, you'll make that move. That
  9. #forces the other player to lose. So if you can get to a losing state, A -> 1, and becomes a winning move
  10. #If, however, you can't ever get to a losing state in one move, that means no matter what you do, the other
  11. #player is going to win. So A -> 0
  12. for j in range(0,x+1): # your prevmove is always in the range 0-k (0 implies starting position)
  13. #so i = current pilesize
  14. #j=prevmove
  15. win=0
  16. for k in range(1,x+1):
  17. if(k==j or k>i):
  18. #you can't make a move here!
  19. continue
  20. else:
  21. if values[(i-k,k)]==0:
  22. win=1
  23. values[(i,j)]=win
  24. return values
  25.  
  26.  
  27. f=open('cookies.txt','r')
  28. f.readline()
  29. count=0
  30. cases=[]
  31. for line in f:
  32. t=line.split()
  33. cases.append((int(t[1]),int(t[0])))
  34. cases=sorted((cases))
  35. k=0
  36. values = {}
  37. count=0
  38. times=0
  39. for x in cases:
  40. times+=1
  41. if x[0]!=k:
  42. values=gendp(x[0])
  43. if(x[0]==3):
  44. print (values)
  45. k=x[0]
  46. if(values[x[1],0]==0):
  47. print (str(x[1])+" "+str(x[0]))
  48. count+=values[(x[1],0)]
  49. print(count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement