bool get = false;
void Start ()
{
ExchangeWater(12, 9, 5, 6);
// ExchangeWater(10, 7, 3, 5);
}
/// <summary>
/// 12升水,有个9升,和5升的空桶,让你取出6升水
/// </summary>
/// <param name="fullCansLitre"></param>
/// <param name="emptyCansLitre1"></param>
/// <param name="emptyCansLitrel2"></param>
/// <param name="evaluation"></param>
private void ExchangeWater(int fullCansLitre, int emptyCansLitre1, int emptyCansLitre2, int evaluation)
{
List<int[]> resoult = new List<int[]>();
Recursion(new int[3] { fullCansLitre, emptyCansLitre1, emptyCansLitre2 }, evaluation, new int[3] { fullCansLitre, 0, 0 }, ref resoult);
}
/// <summary>
/// 三个瓶中某一个向另外两个倾倒水
/// </summary>
/// <param name="one"></param>
/// <param name="two"></param>
/// <param name="evaluation"></param>
/// <param name="resoult"></param>
private void PourAwayTheWater(int[] one, int[] two, int evaluation, ref List<int[]> resoult)
{
int m,n;
for (int i = 0; i < one.Length; i++)
{
m = (i + 1) % 3;
for (int j = 0; j < 2; j++)
{
n = (m + j) % 3;
if (one[i] > 0 && two[n] != one[n])
{
int deltaWater = 0;
int[] tempBottle = new int[3] { two[0], two[1], two[2] };
if (one[n] - two[n] >= two[i])
{
deltaWater = two[i];
}
else
{
deltaWater = one[n] - two[n];
}
tempBottle[i] -= deltaWater;
tempBottle[n] += deltaWater;
Recursion(one, evaluation, tempBottle, ref resoult);
}
}
}
}
/// <summary>
/// 递归向后遍历
/// </summary>
/// <param name="fullCansLitre"></param>
/// <param name="emptyCansLitre1"></param>
/// <param name="emptyCansLitre2"></param>
/// <param name="evaluation"></param>
/// <param name="bottle"></param>
/// <param name="resoult"></param>
private void Recursion(int[] cansLitre, int evaluation, int[] bottle, ref List<int[]> resoult)
{
if (get) return;
if (AdjustCotains(resoult, bottle))
{
// Debug.Log("此结果已出现." + bottle[0] + "," + bottle[1] + "," + bottle[2]);
return;
}
Debug.Log("添加" + bottle[0] + "," + bottle[1] + "," + bottle[2]);
if(bottle[0]== evaluation ||bottle[1]== evaluation ||bottle[2]== evaluation )
{
get = true;
Debug.Log("您要的结果已出现" + bottle[0] + "," + bottle[1] + "," + bottle[2]);
}
resoult.Add(bottle);
PourAwayTheWater(cansLitre, bottle, evaluation, ref resoult);
}
/// <summary>
/// 判断是否已经包含此结果
/// </summary>
/// <param name="resoult"></param>
/// <param name="temp"></param>
/// <returns></returns>
private bool AdjustCotains(List<int[]> resoult,int[] temp)
{
if (resoult.Count == 0)
return false;
int same;
foreach (int[] item in resoult)
{
same = 0;
for (int i = 0; i < item.Length; i++)
{
if (temp[i] == item[i])
{
same++;
}
}
if (same == 3)
return true;
}
return false;
}
void Start ()
{
ExchangeWater(12, 9, 5, 6);
// ExchangeWater(10, 7, 3, 5);
}
/// <summary>
/// 12升水,有个9升,和5升的空桶,让你取出6升水
/// </summary>
/// <param name="fullCansLitre"></param>
/// <param name="emptyCansLitre1"></param>
/// <param name="emptyCansLitrel2"></param>
/// <param name="evaluation"></param>
private void ExchangeWater(int fullCansLitre, int emptyCansLitre1, int emptyCansLitre2, int evaluation)
{
List<int[]> resoult = new List<int[]>();
Recursion(new int[3] { fullCansLitre, emptyCansLitre1, emptyCansLitre2 }, evaluation, new int[3] { fullCansLitre, 0, 0 }, ref resoult);
}
/// <summary>
/// 三个瓶中某一个向另外两个倾倒水
/// </summary>
/// <param name="one"></param>
/// <param name="two"></param>
/// <param name="evaluation"></param>
/// <param name="resoult"></param>
private void PourAwayTheWater(int[] one, int[] two, int evaluation, ref List<int[]> resoult)
{
int m,n;
for (int i = 0; i < one.Length; i++)
{
m = (i + 1) % 3;
for (int j = 0; j < 2; j++)
{
n = (m + j) % 3;
if (one[i] > 0 && two[n] != one[n])
{
int deltaWater = 0;
int[] tempBottle = new int[3] { two[0], two[1], two[2] };
if (one[n] - two[n] >= two[i])
{
deltaWater = two[i];
}
else
{
deltaWater = one[n] - two[n];
}
tempBottle[i] -= deltaWater;
tempBottle[n] += deltaWater;
Recursion(one, evaluation, tempBottle, ref resoult);
}
}
}
}
/// <summary>
/// 递归向后遍历
/// </summary>
/// <param name="fullCansLitre"></param>
/// <param name="emptyCansLitre1"></param>
/// <param name="emptyCansLitre2"></param>
/// <param name="evaluation"></param>
/// <param name="bottle"></param>
/// <param name="resoult"></param>
private void Recursion(int[] cansLitre, int evaluation, int[] bottle, ref List<int[]> resoult)
{
if (get) return;
if (AdjustCotains(resoult, bottle))
{
// Debug.Log("此结果已出现." + bottle[0] + "," + bottle[1] + "," + bottle[2]);
return;
}
Debug.Log("添加" + bottle[0] + "," + bottle[1] + "," + bottle[2]);
if(bottle[0]== evaluation ||bottle[1]== evaluation ||bottle[2]== evaluation )
{
get = true;
Debug.Log("您要的结果已出现" + bottle[0] + "," + bottle[1] + "," + bottle[2]);
}
resoult.Add(bottle);
PourAwayTheWater(cansLitre, bottle, evaluation, ref resoult);
}
/// <summary>
/// 判断是否已经包含此结果
/// </summary>
/// <param name="resoult"></param>
/// <param name="temp"></param>
/// <returns></returns>
private bool AdjustCotains(List<int[]> resoult,int[] temp)
{
if (resoult.Count == 0)
return false;
int same;
foreach (int[] item in resoult)
{
same = 0;
for (int i = 0; i < item.Length; i++)
{
if (temp[i] == item[i])
{
same++;
}
}
if (same == 3)
return true;
}
return false;
}