hello codeforces,↵
↵
I have come here to ask help to dp experts. I will highly appreciate it if you can help solve this dilemma. I have been struggling with this for a while now. ↵
↵
THhe problem can be found here.↵
↵
http://codeforces.net/contest/69/problem/D↵
↵
Now i am not working on a general solution. My algorithm is just for the first test case and i am confused why the result is "Anton loses" when the correct answer is "Anton wins". I have stepped through the logic of my DP formulation and i dont seem to spot the fallacy. So, can you please point out the point where my logic fails. I will highly appreciate it. Thanks for your time. I have commented out parts of my code for legibility. ↵
↵
↵
↵
~~~~~↵
↵
import math↵
↵
↵
#For the following test case↵
#0 0 2 3↵
#The test case that fails is the following↵
↵
~~~~~↵
↵
-100 -100 10 200↵
0 1↵
1 0↵
1 1↵
#31 2↵
#↵
#↵
#x --> x coordinate↵
#y --> y coordin41↵
3 4↵
5 2↵
1 2↵
3 3↵
9 8↵
10 2↵
~~~~~↵
↵
↵
Code starts below↵
↵
~~~~~↵
↵
import mateh↵
#t --> if t == True then Anton turn, if t == False then Dasha turn↵
#fs --> if first player or Anton has swapped, since only 1 swap is possible↵
#ss --> if second player or Dasha has swapped↵
↵
vectors = []↵
↵
line_parts = raw_input().split()↵
x = int(line_parts[0])↵
y = int(line_parts[1])↵
n = int(line_parts[2])↵
d = int(line_parts[3])↵
↵
for i in range(n):↵
l = raw_input().split()↵
vectors.append((int(l[0]),since only 1 swap is possiblet(l[1])))↵
↵
mp = {}↵
↵
↵
def canWin(x, y, t, fs, ss):↵
↵
if math.sqrt(float(x)*float(x) + float(y)*float(y)) > 3:↵
return False↵
↵
math.pow(x, 2) + math.pow(y, 2)) > float(d):↵
return False↵
↵
if (x, y, t) in mp:↵
return mp[(x, y, t)] ↵
↵
result = True ↵
↵
if t == False and ss == False: #if Dasha turn and if dasha hasnt swapped then can swap 1 time↵
return (not (canWin(x + 1, y + 1, not t, fs, ss)))for v in vectors:↵
result = result and (not (canWin(x +1v[0], y + 2v[1], not t, fs, ss)))↵
result = result and (not canWin(y, x, not t, fs, True)) #swap step↵
elif t == True and fs == False: #if Anton turn and if Anton hasnt swapped then can swap 1 time↵
return (not (canWin(x + 1, y + 1, not t, fs, ss)))for v in vectors:↵
result = result and (not (canWin(x +1v[0], y + 2v[1], not t, fs, ss)))↵
result = result and (not canWin(y, x, not t, True, ss)) #swap step↵
else: #since both has swapped 1 time now cannot swap further↵
return (not (canWin(x + 1, y + 1, not t, fs, ss)))for v in vectors:↵
result = result and (not (canWin(x +1v[0], y + 2v[1], not t, fs, ss)))↵
↵
↵
↵
print canWin(0 mp[(x, 0y, True, False, False)↵
↵
#Expected result was True since Anton wins but thet)] = result↵
↵
return result↵
↵
↵
resultis= False↵
↵
↵
~~~~~↵
↵
Also, please note that i have removed the memoization parfor v in vectors:↵
result = resultfor now as to make the code short and easy to understand so i guess this is not a dp solution. But i am just trying to find the fallacy in my recurrence formula. Thanks!canWin(x + v[0], y + v[1], False, False, False) ↵
↵
if result == True:↵
print "Anton"↵
else:↵
print "Dasha"↵
↵
~~~~~↵
↵
I have come here to ask help to dp experts. I will highly appreciate it if you can help solve this dilemma. I have been struggling with this for a while now. ↵
↵
T
↵
http://codeforces.net/contest/69/problem/D↵
↵
↵
↵
~~~~~↵
↵
import math↵
↵
↵
#For the following test case↵
#0 0 2 3↵
#
↵
~~~~~↵
↵
-100 -100 10 200↵
0 1↵
1 0↵
1 1↵
#↵
#↵
#x --> x coordinate↵
#y --> y coordin
3 4↵
5 2↵
1 2↵
3 3↵
9 8↵
10 2↵
~~~~~↵
↵
↵
Code starts below↵
↵
~~~~~↵
↵
import mat
#fs --> if first player or Anton has swapped, since only 1 swap is possible↵
#ss --> if second player or Dasha has swapped
↵
vectors = []↵
↵
line_parts = raw_input().split()↵
x = int(line_parts[0])↵
y = int(line_parts[1])↵
n = int(line_parts[2])↵
d = int(line_parts[3])↵
↵
for i in range(n):↵
l = raw_input().split()↵
vectors.append((int(l[0]),
↵
mp = {}↵
↵
↵
def canWin(x, y, t, fs, ss):↵
↵
if math.sqrt(
return False↵
↵
return False↵
↵
if (x, y, t) in mp:↵
return mp[(x, y, t)] ↵
↵
result = True ↵
↵
if t == False and ss == False: #if Dasha turn and if dasha hasnt swapped then can swap 1 time↵
result = result and (not (canWin(x +
result = result and (not canWin(y, x, not t, fs, True)) #swap step↵
elif t == True and fs == False: #if Anton turn and if Anton hasnt swapped then can swap 1 time↵
result = result and (not (canWin(x +
result = result and (not canWin(y, x, not t, True, ss)) #swap step↵
else: #since both has swapped 1 time now cannot swap further↵
result = result and (not (canWin(x +
↵
↵
print canWin(0
↵
#Expected result was True since Anton wins but the
↵
return result↵
↵
↵
result
↵
~~~~~↵
↵
Also, please note that i have removed the memoization par
result = result
↵
if result == True:↵
print "Anton"↵
else:↵
print "Dasha"↵
↵
~~~~~↵