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 = sel