题目描述:
一个桶中有12斤油,要求倒出6斤,可现在另外只有两个桶,分别可装8斤与5斤,请问应如何来倒?
思路:
广度优先搜索
c#代码:
private void button5_Click(object sender, EventArgs e)
{
//初始状态和结束状态
int[] begin = new int[] { 12, 0, 0 };
int end = 6;
int[][] allStatus = new int[][] { begin };
//所有步骤都记录在logHash里面
Dictionary<int, int> logHash = new Dictionary<int, int>();
int hashCode = GetHashCode(begin);
logHash.Add(hashCode, hashCode);
PouringStep(allStatus, end, logHash);
hashCode = -1;
int value = 0;
List<string> logStep = new List<string>();
while (true)
{
logHash.TryGetValue(hashCode, out value);
if(hashCode != -1)
logStep.Add(hashCode.ToString(