defsolve(n):defdfs(d,state):nonlocal ok,max_d
if d==max_d:
if n in state:ok = True;
return
u = max(state)
if u<<(max_d-d)<n:returnfor op in [1,-1]:
for j in range(d,-1,-1):
next = state[d]+op*state[j]
if next<=0or next in state:continueif u>n and next>n:continue
state[d+1] = next
dfs(d+1,state)
if ok:return
max_d = 0
ok = Falsefor max_d in range(1,20):
ok = False
dfs(0,[1]+[0]*(max_d))
print(max_d)
if ok:print(max_d,);break
BFS的代码
defsolve_3(n):defbfs():
state = deque()
vsit = set()
vsit.add((1,))
state.append((1,))
while len(state[-1]):
now = state.popleft()
if n in now:return len(now)
for i in range(len(now)-1,-1,-1):
for op in [1,-1]:
next=tuple(sorted(now+(now[-1]+now[i]*op,)))
if next notin vsit and next[-1]>0and next[-1] notin now:
vsit.add(next)
state.append(next)
print(bfs()-1)
defsolve(n,missing):defrow_match(x,y):return (2*n+1)*x+y
defcol_match(x,y):return (2*n+1)*x+y+n
definit():for v in missing:exists[v-1] = 0
remains,maxsize = 0,60
exists,matches = [1]*2*n*(n+1),[[] for i in range(2*n*(n+1))]
size,fullsize = [0]*maxsize,[0]*maxsize
contains = {}
s = 0
init()
for L in range(1,n+1):
for x in range(0,n-L+1):#x from 0,n-Lfor y in range(0,n-L+1):#y from 0,n-L
contains[s] = set()
fullsize[s] = 4*L
for j in range(0,L):#j from 0,L-1
up = row_match(x,y+j)
down = row_match(x+L,y+j)
left = col_match(x+j,y)
right = col_match(x+j,y+L)
e = [up,down,left,right]
for t in e:
contains[s].add(t)
matches[t].append(s)
size[s] += exists[up]+exists[down]+exists[left]+exists[right]
if size[s]==fullsize[s]:remains+=1
s+=1deffind_square():for i in range(s):
if size[i]==fullsize[i]:return i
return -1defprocess_edge():for i,x in enumerate(matches):
num = 0for squ in x:
if size[squ]==fullsize[squ]:num+=1
matches[i] = num
defdfs(d,remains):nonlocal maxd,s,ok
if d==maxd:
if find_square()==-1:ok = Truereturn
most_destroyed = max(matches)
if (maxd-d)*most_destroyed < remains:return
k = find_square()
for edge in contains[k]:
for j in range(s):
if edge in contains[j]:
if size[j]==fullsize[j]:
remains -= 1
matches[edge]-=1#square j is complete
size[j] -= 1
dfs(d+1,remains)
if ok:returnfor j in range(s):
if edge in contains[j]:
if size[j]==fullsize[j]:
remains +=1
matches[edge]+=1#the square j is complete
size[j] += 1
ok = False
process_edge()
for maxd in range(1,n*n):
ok = False
dfs(0,remains)
print(maxd)
if ok:print(maxd);break
defsolve(n,missing):defrow_match(x,y):return (2*n+1)*x+y
defcol_match(x,y):return (2*n+1)*x+y+n
definit():for v in missing:exists[v-1] = 0
remains,maxsize = 0,60
exists,matches = [1]*2*n*(n+1),[[] for i in range(2*n*(n+1))]
size,fullsize = [0]*maxsize,[0]*maxsize
contains = {}
s = 0
init()
for L in range(1,n+1):
for x in range(0,n-L+1):#x from 0,n-Lfor y in range(0,n-L+1):#y from 0,n-L
contains[s] = set()
fullsize[s] = 4*L
for j in range(0,L):#j from 0,L-1
up = row_match(x,y+j)
down = row_match(x+L,y+j)
left = col_match(x+j,y)
right = col_match(x+j,y+L)
e = [up,down,left,right]
for t in e:
contains[s].add(t)
matches[t].append(s)
size[s] += exists[up]+exists[down]+exists[left]+exists[right]
if size[s]==fullsize[s]:remains+=1
s+=1deffind_square():for i in range(s):
if size[i]==fullsize[i]:return i
return -1defprocess_edge():for i,x in enumerate(matches):
num = 0for squ in x:
if size[squ]==fullsize[squ]:num+=1
matches[i] = num
defdfs_1(d,remains):nonlocal best
if d>= best:return
most_destroyed = max(matches)
if (best-d)*most_destroyed < remains:return
k = find_square()
if k==-1:best=d;print(best);returnfor edge in contains[k]:
for j in range(s):
if edge in contains[j]:
if size[j]==fullsize[j]:
remains -= 1
matches[edge]-=1#square j is complete
size[j] -= 1
dfs_1(d+1,remains)
for j in range(s):
if edge in contains[j]:
size[j] += 1if size[j]==fullsize[j]:
remains +=1
matches[edge]+=1#the square j is complete
process_edge()
print(matches)
ok = False
best = n*n
dfs_1(0,remains)
print(best)