2020 Ali written test (poker)

Title Description

A stack of cards, each card between 1 and 10

There are four cards method:
single out one
out of 2 Zhang Duizai
a five Zhang Shunzi, such as 12345
out of three with a pair, such as 112233
to number 10, represents 1-10 each brand there are a few, at least to ask how much times can play out

Thinking

Memory of pruning Search +

def dfs(state):
    if tuple(state) in memo:
        return memo[tuple(state)]
    if sum(state) == 0:
        return 0
    else:
        res = float('inf')
        for i in range(10):
            # 出顺子
            if i < 6 and state[i] > 0 and state[i + 1] > 0 and state[i + 2] > 0 and state[i + 3] > 0 and state[
                i + 4] > 0:
                state[i] -= 1
                state[i + 1] -= 1
                state[i + 2] -= 1
                state[i + 3] -= 1
                state[i + 4] -= 1
                res = min(res, dfs(state) + 1)
                state[i] += 1
                state[i + 1] += 1
                state[i + 2] += 1
                state[i + 3] += 1
                state[i + 4] += 1
                memo[tuple(state)] = res
            # 出三连对子
            if i < 8 and state[i] > 1 and state[i + 1] > 1 and state[i + 2] > 1:
                state[i] -= 2
                state[i + 1] -= 2
                state[i + 2] -= 2
                res = min(res, dfs(state) + 1)
                state[i] += 2
                state[i + 1] += 2
                state[i + 2] += 2
                memo[tuple(state)] = res
            # 出2张对子
            if res == float('inf') and state[i] > 1:
                    state[i] -= 2
                    res = min(res, dfs(state) + 1)
                    state[i] += 2
                    memo[tuple(state)] = res
            # 单出一张
            if res == float('inf') and state[i] > 0:
                state[i] -= 1
                res = min(res, dfs(state)+1)
                state[i] += 1
                memo[tuple(state)] = res
        return res
memo = {}
# state = list(map(int, input().split()))
state = [4,4,4,4,4,4,4,4,3,3]
print(dfs(state))
Published 19 original articles · won praise 29 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41987033/article/details/104996509