C#算法求教
如图示:C1、C2、C3为三个容器,内分别分层放置了图示各物体(OBJ1、OBJ2...)。现需将各容器内的物体用对应的器具(T1、T2...)依次取出。求算法!要求:1...
如图示:C1、C2、C3为三个容器,内分别分层放置了图示各物体(OBJ1、OBJ2...)。现需将各容器内的物体用对应的器具(T1、T2...)依次取出。求算法!
要求:
1、各容器内的物体必须依次取出
2、器具需与物体编号对应(如T1-.>OBJ1...)
3、确保最少更换器具取出所有物体
最优结果为
C1-OBJ1
C2-OBJ1
C3-OBJ1
C3-OBJ2
C3-OBJ4
C2-OBJ4
C2-OBJ3
C1-OBJ3
C1-OBJ4
C2-OBJ4
C2-OBJ6
C3-OBJ6
C3-OBJ5
C1-OBJ5
C3-OBJ8 展开
要求:
1、各容器内的物体必须依次取出
2、器具需与物体编号对应(如T1-.>OBJ1...)
3、确保最少更换器具取出所有物体
最优结果为
C1-OBJ1
C2-OBJ1
C3-OBJ1
C3-OBJ2
C3-OBJ4
C2-OBJ4
C2-OBJ3
C1-OBJ3
C1-OBJ4
C2-OBJ4
C2-OBJ6
C3-OBJ6
C3-OBJ5
C1-OBJ5
C3-OBJ8 展开
2个回答
展开全部
class Program
{
static void Main(string[] args)
{
IList<CusObject> c1 = new List<CusObject>();
IList<CusObject> c2 = new List<CusObject>();
IList<CusObject> c3 = new List<CusObject>();
c1.Add(new CusObject() { index = 1 });
c1.Add(new CusObject() { index = 3 });
c1.Add(new CusObject() { index = 4 });
c1.Add(new CusObject() { index = 5 });
c2.Add(new CusObject() { index = 1 });
c2.Add(new CusObject() { index = 4 });
c2.Add(new CusObject() { index = 3 });
c2.Add(new CusObject() { index = 4 });
c2.Add(new CusObject() { index = 6 });
c3.Add(new CusObject() { index = 1 });
c3.Add(new CusObject() { index = 2 });
c3.Add(new CusObject() { index = 4 });
c3.Add(new CusObject() { index = 6 });
c3.Add(new CusObject() { index = 5 });
c3.Add(new CusObject() { index = 8 });
ArrayList list = new ArrayList();
int takeInt = 1;
while (true)
{
if (c1.Count() < 1 && c2.Count() < 1 && c3.Count() < 1) break;
CusObject[] c1Objs =
takeObjects(c1, takeInt);
CusObject[] c2Objs =
takeObjects(c2, takeInt);
CusObject[] c3Objs =
takeObjects(c3, takeInt);
CusObject[] merger = new CusObject[0];
mergerTo(ref merger, c1Objs);
mergerTo(ref merger, c2Objs);
mergerTo(ref merger, c3Objs);
foreach (CusObject obj in merger)
list.Add(obj);
takeInt++;
}
//test
foreach (CusObject obj in list)
{
Console.WriteLine(obj.index);
}
Console.Read();
}
public static void mergerTo(ref CusObject[] OldObjects, CusObject[] mergerObjects)
{
if (mergerObjects == null || mergerObjects.Count() < 1) return;
CusObject[] NewObjects = new CusObject[OldObjects.Length + mergerObjects.Length];
OldObjects.CopyTo(NewObjects, 0);
mergerObjects.CopyTo(NewObjects, OldObjects.Length);
OldObjects = NewObjects;
}
public static CusObject[] takeObjects(IList<CusObject> c,int index)
{
if (c.Count() < 1) return null;
var objects = c.ToArray().Where(w => w.index == index);
foreach (var obj in objects)
c.Remove(obj);
return objects.ToArray();
}
}
public class CusObject
{
public int index { get; set; }
}
public class Container
{
public CusObject theObject { get; set; }
}
我也真是无聊啊,半夜看见所谓的算法就滚进来了...
写了20多分钟,不给分对不起我啊
追问
非常感谢。但是好像有问题,结果应该为111244334466558
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询