益智金字塔求解

#from numpy import *
#import psyco
#psyco.full()

shapbase=[[(0,0,0),(1,0,0),(1,1,0),(1,2,0)],[(0,0,0),(1,0,0),(1,1,0),(1,2,0),(0,1,0)], [(0,0,0),(1,0,0),(1,1,0),(1,2,0),(1,3,0)], [(0,1,0),(1,0,0),(1,1,0),(1,2,0),(1,3,0)], \
          [(0,0,0),(0,1,0),(1,1,0),(1,2,0),(1,3,0)], [(0,0,0),(0,1,0),(1,1,0)], [(0,0,0),(0,1,0),(0,2,0),(2,1,0),(2,2,0)], [(0,0,0),(0,1,0),(1,1,0),(2,1,0),(2,2,0)], \
          [(0,0,0),(1,0,0),(0,1,0),(0,2,0),(0,2,1)], [(0,0,0),(0,1,0),(0,2,0),(0,3,0)], [(0,0,0),(0,1,0),(1,1,0),(1,1,0)], [(1,0,0),(0,1,0),(1,1,0),(1,2,0),(2,1,0)]]
shapbaser=[[(0,0,1),(1,1,0),(1,1,1),(2,2,2)],[(0,0,1),(1,1,1),(0,0,2),(0,0,3),(1,1,0)], [(0,0,1),(1,1,0),(1,1,1),(1,1,2),(1,1,3)], [(0,0,0),(1,1,0),(0,0,1),(0,0,2),(0,0,3)], \
          [(0,0,0),(1,1,0),(0,0,1),(1,1,1),(1,1,2)], [(0,0,0),(1,1,0),(0,0,1)], [(0,0,0),(2,2,0),(0,0,1),(1,1,1),(0,0,2)], [(2,0,0),(1,1,0),(0,2,0),(1,0,1),(0,1,1)], \
          [(1,1,0),(0,0,1),(1,1,1),(1,1,2),(0,0,3)], [(0,0,0),(0,0,1),(0,0,2),(0,0,3)], [(1,1,0),(0,0,1),(1,1,1),(0,0,2)], [(1,1,0),(2,2,0),(1,1,1),(0,0,2),(1,1,2)]]
shapbase1=[]


for ind in xrange(0, len(shapbase)):
    s=shapbase[ind]
    ss=[]
    ss.append(s)
        
    #x==>-x;y==>y
    t=[]
    for i in s:
        x,y,z=i
        t.append((-x,y,z))
    ss.append(t)

    trs=[]
    for sr in ss:
        trs.append(sr)
        #x==>-y,y==>x
        t=[]
        for i in sr:
            x,y,z=i
            t.append((-y,x,z))
        trs.append(t)
    
        #x==>-x;y==>-y
        t=[]
        for i in sr:
            x,y,z=i
            t.append((-x,-y,z))
        trs.append(t)
        
        #x==>y;y==>-x
        t=[]
        for i in sr:
            x,y,z=i
            t.append((y,-x,z))
        trs.append(t)
            
    s=shapbaser[ind]
    ss=[]
    ss.append(s)
        
    #x==x;y==>y;z==>-z
    t=[]
    for i in s:
        x,y,z=i
        t.append((x-z,y-z,-z))
    ss.append(t)

    for sr in ss:
        trs.append(sr)
        #x==>-y,y==>x
        t=[]
        for i in sr:
            x,y,z=i
            t.append((-y-z,x,z))
        trs.append(t)
    
        #x==>-x;y==>-y
        t=[]
        for i in sr:
            x,y,z=i
            t.append((-x-z,-y-z,z))
        trs.append(t)
        
        #x==>y;y==>-x
        t=[]
        for i in sr:
            x,y,z=i
            t.append((y,-x-z,z))
        trs.append(t)          
    shapbase1.append(trs)
    
def initpos(pos):
    for z in xrange(0,5):
        for x in xrange(0,5-z):
            for y in xrange(0,5-z):
                if 0<=x<5 and 0<=y<5 and z==0:
                    pos[(x,y,z)]=0
                elif 0<=x<4 and 0<=y<4 and z==1:
                    pos[(x,y,z)]=0
                elif 0<=x<3 and 0<=y<3 and z==2:
                    pos[(x,y,z)]=0
                elif 0<=x<2 and 0<=y<2 and z==3:
                    pos[(x,y,z)]=0
                elif 0<=x<1 and 0<=y<1 and z==4:
                    pos[(x,y,z)]=0
                else: pos[(x,y,z)]=20

def initpos0(pos):
    pos[0] = 0
    pos[1] = 0
"""    
    for z in xrange(0,5):
        for x in xrange(0,5-z):
            for y in xrange(0,5-z):
                if 0<=x<5 and 0<=y<5 and z==0:
                    pos[(x,y,z)]=0
                elif 0<=x<4 and 0<=y<4 and z==1:
                    pos[(x,y,z)]=0
                elif 0<=x<3 and 0<=y<3 and z==2:
                    pos[(x,y,z)]=0
                elif 0<=x<2 and 0<=y<2 and z==3:
                    pos[(x,y,z)]=0
                elif 0<=x<1 and 0<=y<1 and z==4:
                    pos[(x,y,z)]=0
                else: pos[(x,y,z)]=20
"""

def setpos(v,pos,shape,(x,y,z)):
    for s in shape:
        if s[0]+x<0 or s[1]+y<0 or s[2]+z<0: return None
        if s[0]+x>4 or s[1]+y>4 or s[2]+z>4: return None

        if (s[0]+x>3 or s[1]+y>3) and s[2]+z==1: return None
        if (s[0]+x>2 or s[1]+y>2) and s[2]+z==2: return None
        if (s[0]+x>1 or s[1]+y>1) and s[2]+z==3: return None
        if (s[0]+x>0 or s[1]+y>0) and s[2]+z==4: return None
        if pos[(s[0]+x,s[1]+y,s[2]+z)] != 0:
            return None
    newpos=pos.copy()
    for s in shape:
        newpos[(s[0]+x,s[1]+y,s[2]+z)] = v
    return newpos

indB=[0,25,41,50,54]
indy=[5,4,3,2,1]

def setdot(pos, (x,y,z)):
    indp=indB[z]+indy[z]*y+x
    pos[indp/32]|=1<<(indp%32);

def getdot(pos, (x,y,z)):
    indp=indB[z]+indy[z]*y+x
    return (pos[indp/32]>>(indp%32))&1;

def setpos0(v,pos,shape,(x,y,z)):
    for s in shape:
        if s[0]+x<0 or s[1]+y<0 or s[2]+z<0: return None
        if s[0]+x>4 or s[1]+y>4 or s[2]+z>4: return None

        if (s[0]+x>3 or s[1]+y>3) and s[2]+z==1: return None
        if (s[0]+x>2 or s[1]+y>2) and s[2]+z==2: return None
        if (s[0]+x>1 or s[1]+y>1) and s[2]+z==3: return None
        if (s[0]+x>0 or s[1]+y>0) and s[2]+z==4: return None
        
        if getdot(pos, (s[0]+x,s[1]+y,s[2]+z)) != 0:
            return None
    newpos=pos[:]
    for s in shape:
        setdot(newpos, (s[0]+x,s[1]+y,s[2]+z))
    return newpos

def trypos(pos,initpos):
    res=[]
    for t in shapbase1[pos]:
        for x in xrange(-10,10):
            for y in xrange(-10,10):
                for z in xrange(-10,10):
                    initbak=setpos0(pos+1, initpos, t, (x,y,z))
                    if  initbak != None:
                        res.append(initbak)
    return res

def dis0(pos):
    print "="*12
    for i in xrange(5,0,-1):
        for j in xrange(0,i):
            s=""
            for k in xrange(0,i):
                s=s+" %d"%(getdot(pos,(j,k,5-i)))
            print s
        print "-"*i*2

def dis(pos):
    print "="*12
    for i in xrange(5,0,-1):
        for j in xrange(0,i):
            s=""
            for k in xrange(0,i):
                s=s+" %d"%pos[(j,k,5-i)]
            print s
        print "-"*i*2


#print "0", len(res),res[0][(0,0,0)]
def getres(initpos, start, end):
    res=[initpos]
    for i in xrange(start, end):
        resnext=[]
        for t in res:
            resnext.extend(trypos(i,t)[:])
        res=resnext
        print "resolve %d..len %d."%(i, len(res))
#        for r in res:
#            dis(r)
#        break
    return res

def combine(res1, res2, res3, res4):
    res=[]
    cnt = 0
    for s in res1:
        print "combine...", cnt, len(res1)
        cnt+=1
        for t in res2:
            conflict1=0
            for z in xrange(0,5):
                if conflict1==1 : break
                for x in xrange(0,5-z):
                    if conflict1==1 : break
                    for y in xrange(0,5-z):
                        if s[(x,y,z)] * t[(x,y,z)]  !=  0:
                            conflict1=1
                            break
            if conflict1==1:continue
            
            for u in res3:
                conflict1=0
                for z in xrange(0,5):
                    if conflict1==1 : break
                    for x in xrange(0,5-z):
                        if conflict1==1 : break
                        for y in xrange(0,5-z):
                            if s[(x,y,z)]*u[(x,y,z)] + t[(x,y,z)]*u[(x,y,z)] !=  0:
                                conflict1=1
                                break
                if conflict1==1:continue
                
                for v in res4:
                    conflict=0
                    for z in xrange(0,5):
                        if conflict==1 : break
                        for x in xrange(0,5-z):
                            if conflict==1 : break
                            for y in xrange(0,5-z):
                                if (s[(x,y,z)]*v[(x,y,z)] + t[(x,y,z)]*v[(x,y,z)] + u[(x,y,z)]*v[(x,y,z)] ) !=  0:
                                    conflict=1
                                    break
                    if conflict==1:continue
                    newpos=s.copy()
                    for z in xrange(0,5):
                        for x in xrange(0,5-z):
                            for y in xrange(0,5-z):
                                if t[(x,y,z)] != 0:
                                    newpos[(x,y,z)] = t[(x,y,z)]  
                                if u[(x,y,z)] != 0:
                                    newpos[(x,y,z)] = u[(x,y,z)]  
                                if v[(x,y,z)] != 0:
                                    newpos[(x,y,z)] = v[(x,y,z)]  
                    res.append(newpos)
    return res

def combine0(res1, res2, res3, res4, res5, res6):
    res=[]
    cnt = 0
    for s in res1:
        print "combine...", cnt, len(res1)
        cnt+=1
        for t in res2:
            if s[0]&t[0]!=0 or s[1]&t[1]!=0: continue

            for u in res3:
                if s[0]&u[0]!=0 or s[1]&u[1]!=0 or t[0]&u[0]!=0 or t[1]&u[1]!=0: continue

                for v in res4:
                    if s[0]&v[0]!=0 or s[1]&v[1]!=0 or t[0]&v[0]!=0 or t[1]&v[1]!=0 or u[0]&v[0]!=0 or u[1]&v[1]!=0: continue

                    for v1 in res5:
                        if s[0]&v1[0]!=0 or s[1]&v1[1]!=0 or t[0]&v1[0]!=0 or t[1]&v1[1]!=0 or u[0]&v1[0]!=0 or u[1]&v1[1]!=0\
                           or v[0]&v1[0]!=0 or v[1]&v1[1]!=0 : continue

                    
                        for v2 in res6:
                            if s[0]&v2[0]!=0 or s[1]&v2[1]!=0 or t[0]&v2[0]!=0 or t[1]&v2[1]!=0 or u[0]&v2[0]!=0 or u[1]&v2[1]!=0\
                            or v[0]&v2[0]!=0 or v[1]&v2[1]!=0 or v1[0]&v2[0]!=0 or v1[1]&v2[1]!=0 : continue

                            print "get a result...", dis0(t), dis0(u), dis(v), dis(v1), dis(v2)
    return res

pos=[0,0]
#initpos0(pos)


res02=getres(pos, 0, 2)
res24=getres(pos, 2, 4)
res46=getres(pos, 4, 6)
res68=getres(pos, 6, 8)
res810=getres(pos, 8, 10)
res1012=getres(pos, 10, 12)
res08=combine0(res02, res24, res46, res68, res810, res1012)
print "done..."
#    for r in res:
#        dis(r)
#print len(shapbase1[0])


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值