DFS 求解金字塔10*10等腰直角三角形摆放

# -*- coding:utf8 -*-
shape={
       0:[(0,0),(1,0),(1,1),(1,2)],
       1:[(0,0),(0,1),(1,0),(1,1),(1,2)],
       2:[(0,0),(1,0),(1,1),(1,2),(1,3)],
       3:[(0,1),(1,0),(1,1),(1,2),(1,3)],

       4:[(0,0),(0,1),(1,1),(1,2),(1,3)],
       5:[(0,0),(1,0),(1,1)],
       6:[(0,0),(1,0),(2,0),(2,1),(2,2)],
       7:[(0,0),(1,0),(1,1),(2,1),(2,2)],

       8:[(0,0),(0,1),(1,0),(2,0),(2,1)],
       9:[(0,0),(0,1),(0,2),(0,3)],
       10:[(0,0),(1,0),(1,1),(0,1)],
       11:[(1,0),(0,1),(1,1),(1,2),(2,1)],
}
allshape={}
field=[]
prepos=[]

def rotate(v, d):
       ibin=[0]*5
       rv0=max([vi[0] for vi in v])
       rv1=max([vi[1] for vi in v])
       dd=d/4
       d%=4
       ta= 1 if d<2 else -1
       tb= -1 if (d%2)==1 else 1
       for vi in v:
              v0= vi[0] if ta>0 else (rv0+vi[0]*ta)
              v1= vi[1] if tb>0 else (rv1+vi[1]*tb)
              v0,v1 = (v1,v0) if dd==1 else (v0,v1)
              ibin[v1] |= (1<0:
              ibin=map(lambda x:x<<1, ibin)

       trim=0
       if ibin[trim]==0:
              trim+=1
       ibin=ibin[trim:]
       trim=0
       while trim0:
                            if c&1 == 1:
                                   fields[b[2]+y] = fields[b[2]+y][:pos+b[1]] +  chr(ord('a')+i) + fields[b[2]+y][pos+b[1]+1:]
                            pos+=1
                            c>>=1
       for col,f in enumerate(fields):
              f=f[:col+1]
              for i in xrange(12):
                     f=f.replace(chr(ord('a')+i), ["❤️","?","?","?","?","?","?","?","?","?","?","?"][i])
              f=f.replace(' ','⭕')
              print f
       #
       # print "----"*4
       #
       # for f in binfield:
       #        print bin(f)[2:]
       print "===="*4
def addoneshape(target, current, used, shapeid, shape, pos):
       pass

def rmoneshape(current, used, shapeid, shape):
       pass

def tryfeed(targetmap, ks):
       feed=[0]*10
       check1=[0]*10
       check2=[0]*10
       bakcheck=[0]*10
       for i,onek in enumerate(ks):
           block=onek[0]
           x0=onek[1]
           y0=onek[2]
           for y,b in enumerate(block):
                  if y0+y>=10: return False
                  feed[y0+y] |= b<>1
                     tmp2=tmp>>2
                     tmp3=tmp>>3
                     if i>0:
                            if (tmp&lasth[i-1]) == 0 and (tmp&lasth[i+1])==0 and (tmp0&lasth[i])==0:
                                   if tmp1&lasth[i] == 0: return True
                                   if tmp2&lasth[i]==0 and tmp1&lasth[i-1]==0 and (tmp1&lasth[i+1])==0: return True
                                   if tmp3&lasth[i]==0 and tmp1&lasth[i-1]==0 and tmp1&lasth[i+1]==0 \
                                       and tmp2&lasth[i-1] and tmp2&lasth[i+1]: return True
                     if i>1:
                            if tmp&lasth[i-2]==0 and tmp&lasth[i+1]==0 \
                                and tmp0&lasth[i-1]==0 and tmp1&lasth[i-1]==0 \
                                and tmp0&lasth[i]==0 and tmp1&lasth[i]==0:
                                   return True
                     if i>2:
                            if tmp&lasth[i-3]==0 and tmp&lasth[i+1]==0 \
                                and tmp0&lasth[i-1]==0 and tmp1&lasth[i-1]==0 \
                                and tmp0&lasth[i-2]==0 and tmp1&lasth[i-2]==0 \
                                and tmp0&lasth[i]==0 and   tmp1&lasth[i]==0:
                                   return True
                     tmp=oldtmp

       return False


import itertools
maxdeep=0
def findans():
       targetfield=[]
       for i in xrange(0,10):
              targetfield.append(((1<<(i+1))-1))

       allshapes = allshape.values()
       print "try......", targetfield
       def putoneblock(targetfield, shapes, putblock):
              global maxdeep
              if len(shapes) == 0:
                     print putblock
                     return True
              if len(putblock)>maxdeep:
                     maxdeep=len(putblock)
                     if maxdeep>=10:
                            print putblock
                            dumpput(putblock)

                     print "deep:",maxdeep
              for s in shapes[0]:
                     for x in xrange(10):
                            for y in xrange(10):
                                   backfield=targetfield[:]
                                   passput=False
                                   for ybase,sc in enumerate(s):
                                          if y+ybase>=10:
                                                 passput=True
                                                 break
                                          checkv=targetfield[y+ybase]
                                          targetfield[y+ybase]^=sc<

DFS 求解金字塔10*10等腰直角三角形摆放_第1张图片

你可能感兴趣的:(算法,flutter,python)