以下是每个起始位置的解决方案:
Start 1: (2, 0, -1) Reds=1 Blues=0
Red B ==> (3, -1, -2) Reds=1 Blues=0
Red B ==> (4, -2, -3) Reds=1 Blues=0
Red D ==> (4, 0, -2) Reds=0 Blues=2
Blue A ==> (2, 0, -1) Reds=0 Blues=1
Blue A ==> (0, 0, 0) Reds=0 Blues=0
Start 2: (3, 1, -1) Reds=2 Blues=0
Red A ==> (3, -1, -2) Reds=1 Blues=0
Red B ==> (4, -2, -3) Reds=1 Blues=0
Red D ==> (4, 0, -2) Reds=0 Blues=2
Blue A ==> (2, 0, -1) Reds=0 Blues=1
Blue A ==> (0, 0, 0) Reds=0 Blues=0
以下是一段使用C#编写的程序,进行随机游走,在执行了少量步骤后便放弃搜索。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SO8683939
{
struct State
{
public int V1;
public int V2;
public int V3;
public int Reds;
public int Blues;
public int Tokens { get { return Reds + Blues; } }
public string Description;
public State(int v1, int v2, int v3, int reds, int blues)
{
V1 = v1;
V2 = v2;
V3 = v3;
Reds = reds;
Blues = blues;
Description = null;
}
public State Add(State other)
{
State sum;
sum.V1 = V1 + other.V1;
sum.V2 = V2 + other.V2;
sum.V3 = V3 + other.V3;
sum.Reds = Reds + other.Reds;
sum.Blues = Blues + other.Blues;
sum.Description = null;
return sum;
}
public override string ToString()
{
var detail = string.Format("({0}, {1}, {2}) Reds={3} Blues={4}", V1, V2, V3, Reds, Blues);
if (Description != null)
{
return Description + ": " + detail;
}
return detail;
}
}
class Program
{
static void Main(string[] args)
{
var start1 = new State(2, 0, -1, 1, 0) { Description = "Start 1" };
var start2 = new State(3, 1, -1, 2, 0) { Description = "Start 2" };
var end = new State(0, 0, 0, 0, 0);
var redA = new State(0, -2, -1, -1, 0) { Description = "Red A" };
var redB = new State(1, -1, -1, 0, 0) { Description = "Red B" }; ;
var redC = new State(2, 0, -1, 1, 0) { Description = "Red C" }; ;
var redD = new State(0, 2, 1, -1, 2) { Description = "Red D" }; ;
var redOptions = new[] { redA, redB, redC, redD };
var blueA = new State(-2, 0, 1, 0, -1) { Description = "Blue A" };
var blueB = new State(-1, 1, 1, 0, 0) { Description = "Blue B" };
var blueC = new State(0, 2, 1, 0, 1) { Description = "Blue C" };
var blueD = new State(2, 0, -1, 2, -1) { Description = "Blue D" };
var blueOptions = new[] { blueA, blueB, blueC, blueD };
var startingPosition = start1;
var maxSolutionLength = 5;
var rand = new Random();
var path = new List<State>();
while (true)
{
var current = startingPosition;
path.Clear();
while (true)
{
State selected;
if (current.Reds == 0)
{
selected = blueOptions[rand.Next(4)];
}
else if (current.Blues == 0)
{
selected = redOptions[rand.Next(4)];
}
else
{
if (rand.NextDouble() < 0.5)
{
selected = blueOptions[rand.Next(4)];
}
else
{
selected = redOptions[rand.Next(4)];
}
}
path.Add(selected);
current = current.Add(selected);
if (current.Equals(end))
{
Console.WriteLine("Success!");
var retrace = startingPosition;
Console.WriteLine(retrace);
foreach (var selection in path)
{
retrace = retrace.Add(selection);
Console.WriteLine("{0} ==> {1}", selection.Description, retrace);
}
Console.ReadLine();
break;
}
else if (current.Tokens == 0)
{
break;
}
else if (path.Count >= maxSolutionLength)
{
break;
}
}
}
}
}
}
(1, 0, 0)
和一个随机颜色的棋子开始吗?此外,在每个步骤中,所有棋子都可用吗?还是每个棋子只能使用一次,或者你会得到一组随机选择的棋子? - templatetypedef