原題如下:12(a桶 滿的 有12斤油)斤桶裡 取出6斤油 有 另外有8斤(b桶)和5斤(c桶)兩個空桶 讓程序輸出取出這6斤油的步驟
現在實現的算法可以配參數(定義有幾個桶,初始有多少油,要得到多少油,都可以配),並且找出任意(多條線路或者找不到)滿足條件的倒油線路圖:
運行效果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//在此自定義相關初始信息
Oil.初始油量 = "12,0,0";
Oil.桶的容量 = "12,8,5";
Oil.需求油量 = "6";
Oil oil = new Oil();
Status SuperStatus = oil.GetStartState();
oil.status.Add(SuperStatus);
oil.執行(SuperStatus);
if (oil.Endstatus.Count == 0)
{
Console.WriteLine("無法倒出指定質量大小的油。");
}
else
{
Console.WriteLine("共找到" + oil.Endstatus.Count.ToString()+"條倒油線路:");
}
for (int i = 0; i < oil.Endstatus.Count; i++)
{
Console.WriteLine(oil.翻譯路線(oil.Endstatus[i]));
}
}
}
class Oil
{
public static string 桶的容量;
public static string 初始油量;
public static string 需求油量;
//自動記錄桶的總數
int count;
public Oil()
{
count = 桶的容量.Split(,).Count();
capacity = new int[count];
for (int i=0;i<count;i++)
{
capacity[i] = Convert.ToInt32(桶的容量.Split(,)[i]);
}
}
public int[] capacity; //new int[] { 12, 8, 5 };
public List<Status> status =