首頁>技術>

names=['人','狼','羊','菜']status=[0,0,0,0]actions=[]def gowith(i):    if status[i]==status[0]:                status[0]=not(status[0])           status[i]=status[0]        loop=0        if(len(actions)>=2):            for k in range(len(actions)-1):                if actions[k][0]==tuple(status):#是否與前面狀態重複,避免形成迴圈                    status[0]=not(status[0])                    status[i]=status[0]                    loop=1                    return 0                        if loop==0:                       if (status[1]==status[2])and(status[1]!=status[0]):#狼吃羊                status[0]=not(status[0])                status[i]=status[0]                 return 0                elif (status[2]==status[3])and(status[2]!=status[0]):#羊吃菜                status[0]=not(status[0])                status[i]=status[0]                return 0        return 1    else:        return 0def gohe(n):    global status    if status==[1,1,1,1] :         print(actions)    else:         i=0        for i in range(4):            oldstatus=tuple(status)              if gowith(i):                if i==0:                    actions.append([tuple(status),(names[0])])                else:                    actions.append([tuple(status),(names[0],names[i])])                gohe(n+1)                actions.pop()                status=list(oldstatus)                                  gohe(1)  

由於人、羊、狼、菜只有兩種轉態,在河這邊或者另一邊,可以看作0和1,那麼整個的狀態可以拼合成一個數字(4位二進位制數),用這個數代替status也是可行的,而且在判斷是否和前面狀態重複時更方便。

15
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 註釋,今晚我不關心程式碼,我只想你