玩蛇网提供最新Python编程技术信息以及Python资源下载!

游戏水桶倒水问题Python语言的解决方法

python 培训

Python编程语言关于游戏水桶倒水问题的解决方法是怎么样的?问题是:有三个水桶,分别是10升、7升和3升。10升的水桶是满的,要求想办法把另外两个桶变成5升的,并且不能浪费水哦!需要用Python来完成这个游戏,大家有什么好的思路吗?

其他Python相关游戏源码文章推荐:Python实现自动扫雷游戏的方法源码

下边先来看看我的水桶倒水问题Python语言的解决方法:

#! /usr/bin/env python

# -*- coding: utf-8 -*-

LIMITS = (10, 7, 3)
INIT = (10, 0, 0)
WIN = (5, 5, 0)

from copy import copy

class State:
    def __init__(self, init_value):
        self.parent = -1
        self.value = init_value
        self.move = (0, 0, 0) #(from, to, amount)

    #spawn by put water around

    def spawn(self):

        for i in range(len(LIMITS)): #for each cup

            if not self.value[i]: continue #if empty cup then next

            for k in range(len(LIMITS)): #otherwise try put water to other cups
                if i == k: continue
                val_i = self.value[i]
                val_k = self.value[k]
                #1. move all in i to k
                #2. move i to make k full
                #so the minimum of the 2 actions is the target
                min_ik = min(val_i, LIMITS[k] - val_k)
                new_value = list(copy(self.value))
                new_value[i] -= min_ik
                new_value[k] += min_ik
                new_value = tuple(new_value)
                if new_value in g_state_map: #already exists?
                    continue
                new_state = State(new_value)
                new_state.parent = g_index
                new_state.move = (i, k, min_ik)
                g_states.append(new_state)
                g_state_map[new_value] = True
                if new_value == WIN:
                    return True
        return False

#www.iplaypython.com

def print_solution(state):
    states = []

    while state.parent != -1:
        states.append(state)
        state = g_states[state.parent]
    states.reverse()

    print "At least:", len(states), "steps:"

    print "(Cup0, Cup1, Cup2) (from, to, amount)"

    print INIT

    for state in states:
        print state.value, state.move

#solve the problem here!
g_states = [State(INIT)] #initially we have (10, 0, 0)
g_state_map = {} #sate:true/false, avoid duplicate states
g_index = 0

while g_index < len(g_states):
    state = g_states[g_index]

    if state.spawn():
        print_solution(g_states[-1])

        break

    g_index += 1

result = """

At least: 9 steps:

(Cup0, Cup1, Cup2) (from, to, amount)

(10, 0, 0)
(3, 7, 0) (0, 1, 7)
(3, 4, 3) (1, 2, 3)
(6, 4, 0) (2, 0, 3)
(6, 1, 3) (1, 2, 3)
(9, 1, 0) (2, 0, 3)
(9, 0, 1) (1, 2, 1)
(2, 7, 1) (0, 1, 7)
(2, 5, 3) (1, 2, 2)
(5, 5, 0) (2, 0, 3)
"""

python代码中的python reverse方法是列表反转排序,具体使用方法可点击链接查看。

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/game/g2615.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-01-29 21:26 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部