疯狂的8——python

    xiaoxiao2022-07-13  156

    本来打算写个界面版的疯狂的8的,但是最近太忙了,抽不出时间来,先标记下用动态规划求解疯狂的8的步骤及代码,以后有空把它封装到上次的发牌界面版里吧。

    举例就是课本上的,输入cards = ['7c', '7h', 'Kc', 'Ks', '8h']对应的就是下图 接下来是求解的过程,请按照课本的思路来理解这里的动态规划。没时间再整理了!抱歉了!步骤简单整理了下,将就着看看: 代码如下: #-*- coding: utf-8 -*- # 随机产生n张扑克 def generate_cards(n): import random import itertools random.seed(1) SUITS = 'cdhs' #四种花色 RANKS = '23456789TJQKA' #十三种面值 DECK = tuple(''.join(card) for card in itertools.product(RANKS, SUITS)) hand = random.sample(DECK, n) return hand import numpy as np def crazy_eight(cards): trick = {} parent = {} trick[0] = 1 parent[0] = None print('Computing i={},trick[{}]={},parent[{}]={}'.format(0, 0, trick[0], 0, parent[0])) for i, ci in enumerate(cards): tem_trick = [] if i > 0: print('Computing i={},ci={}'.format(i, ci)) for j, cj in enumerate(cards[:i]): print('\t j={},cj={}'.format(j, cj)) if is_trick(ci,cj): tem_trick.append(trick[j]) print('\t ci={}~cj={}, trick[{}]={} is appended'.format(ci, cj, j, trick[j])) else: tem_trick.append(0) print('\t ci={}<>cj={},0 is appended'.format(ci, cj)) max_trick = max(tem_trick) trick[i] = 1+max_trick print('\t +++++++++++Computing trick[{}]++++++++++++++++++++++++'.format(i)) print('\t trick[{}]={}=1+max of {}'.format(i, trick[i], tem_trick)) ind_max = np.argmax(tem_trick) print('\t index of max value is {}'.format(ind_max)) if is_trick(ci,cards[ind_max]): parent[i] = ind_max print('\t ci={}~cards[{}], parent[{}]={}'.format(ci, cards[ind_max], i, ind_max)) else: parent[i] = None print('\t ci={}<>{},parent[{}]={}'.format(ci, cards[ind_max], i, None)) return trick, parent # 判断两张扑克是否配对 def is_trick(c1, c2): if c1[0] == c2[0]: return True elif c1[1] == c2[1]: return True elif c1[0] == '8' or c2[0]=='8': return True else: return False def get_longest_subsequence(cards, trick, parent): ind_max = max(trick.keys(), key=(lambda key: trick[key])) subsequence = [] while ind_max is not None: subsequence.append(cards[ind_max]) ind_max = parent[ind_max] subsequence.reverse() return subsequence if __name__ == '__main__': #cards = generate_cards(10) #print("input cards = ",cards) cards = ['7c', '7h', 'Kc', 'Ks', '8h'] trick, parent = crazy_eight(cards) print( trick) sub_cards = get_longest_subsequence(cards, trick, parent) print("longest subcards:", sub_cards,'and sum up to', len(sub_cards)) 结果如下: Computing i=0,trick[0]=1,parent[0]=None Computing i=1,ci=7h j=0,cj=7c ci=7h~cj=7c, trick[0]=1 is appended +++++++++++Computing trick[1]++++++++++++++++++++++++ trick[1]=2=1+max of [1] index of max value is 0 ci=7h~cards[7c], parent[1]=0 Computing i=2,ci=Kc j=0,cj=7c ci=Kc~cj=7c, trick[0]=1 is appended j=1,cj=7h ci=Kc<>cj=7h,0 is appended +++++++++++Computing trick[2]++++++++++++++++++++++++ trick[2]=2=1+max of [1, 0] index of max value is 0 ci=Kc~cards[7c], parent[2]=0 Computing i=3,ci=Ks j=0,cj=7c ci=Ks<>cj=7c,0 is appended j=1,cj=7h ci=Ks<>cj=7h,0 is appended j=2,cj=Kc ci=Ks~cj=Kc, trick[2]=2 is appended +++++++++++Computing trick[3]++++++++++++++++++++++++ trick[3]=3=1+max of [0, 0, 2] index of max value is 2 ci=Ks~cards[Kc], parent[3]=2 Computing i=4,ci=8h j=0,cj=7c ci=8h~cj=7c, trick[0]=1 is appended j=1,cj=7h ci=8h~cj=7h, trick[1]=2 is appended j=2,cj=Kc ci=8h~cj=Kc, trick[2]=2 is appended j=3,cj=Ks ci=8h~cj=Ks, trick[3]=3 is appended +++++++++++Computing trick[4]++++++++++++++++++++++++ trick[4]=4=1+max of [1, 2, 2, 3] index of max value is 3 ci=8h~cards[Ks], parent[4]=3 {0: 1, 1: 2, 2: 2, 3: 3, 4: 4} ('longest subcards:', ['7c', 'Kc', 'Ks', '8h'], 'and sum up to', 4)
    最新回复(0)