public GState(List <string[]> RTGIn, GState parent, int moves, int floor) { RTG = RTGIn.ToList(); Parent = parent; Moves = moves; currentFloor = floor; }
private object Greedy(List <string[]> RTGIn) { Greedy gr = new Greedy(); GState gs = new GState(RTGIn, null, 0, 0); return(gr.iterate(gs) + " (Greedy)"); }
private void Draw(GState gs) { Console.WriteLine($"\nMove: {gs.Moves}"); //System.IO.File.AppendAllText(@"C:\out.txt", $"Move: {moves}/{ctr++}\n"); for (int i = gs.RTG.Count - 1; i > -1; i--) { string a = i == gs.currentFloor ? "e" : ""; Console.WriteLine($"F{i + 1}{a}\t {string.Join(" ", gs.RTG[i])}"); //System.IO.File.AppendAllText(@"C:\out.txt", $"F{i + 1}{a}\t {string.Join("\t", RTG[i])}\n"); } Console.WriteLine(); //System.IO.File.AppendAllText(@"C:\out.txt", $"\n"); }
private List <string> GetPermutations(GState gs) { HashSet <string> tmp = new HashSet <string>(); foreach (var item1 in gs.RTG[gs.currentFloor]) { tmp.Add(item1); foreach (var item2 in gs.RTG[gs.currentFloor]) { if (item1 != item2) { tmp.Add(string.Compare(item1, item2) >= 0 ? $"{item1},{item2}" : $"{item2},{item1}"); } } } return(tmp.Select(a => a).OrderBy(a => a.Length).Reverse().ToList());; }
public bool CheckIfValid(GState gs) { string result = ""; foreach (var(floor, index) in gs.RTG.WithIndex()) { result += $""; result += string.Join($"", floor.OrderBy(a => a).ToArray()); result += $"|"; } gs.value = result; if (history.Contains(result)) { return(false); } history.Add(result); return(true); }
public int iterate(GState gs) { if (gs.Moves > maxIteration) { return(0); } if (gs.RTG[3].Count() == gs.RTG.SelectMany(a => a).Count()) { if (gs.Moves <= maxIteration) { maxIteration = gs.Moves; } return(0); } else { var perms = GetPermutations(gs); gs.Moves++; foreach (var item in perms) { var items = item.Split(","); if (gs.currentFloor == 0 || gs.currentFloor == 1 || gs.currentFloor == 2) { var dupGs = new GState(gs.RTG, gs, gs.Moves, gs.currentFloor + 1); List <string> listThis = new List <string>(dupGs.RTG[gs.currentFloor]); List <string> listUp = new List <string>(dupGs.RTG[gs.currentFloor + 1]); foreach (var q in items) { listThis.Remove(q); listUp.Add(q); } dupGs.RTG[gs.currentFloor] = listThis.ToArray(); dupGs.RTG[gs.currentFloor + 1] = listUp.ToArray(); if (CheckIfValid(dupGs)) { iterate(dupGs); } } if (gs.currentFloor == 3 || gs.currentFloor == 1 || gs.currentFloor == 2) { var dupGs2 = new GState(gs.RTG, gs, gs.Moves, gs.currentFloor - 1); List <string> listThis = new List <string>(gs.RTG[gs.currentFloor]); List <string> listDown = new List <string>(gs.RTG[gs.currentFloor - 1]); foreach (var q in items) { listThis.Remove(q); listDown.Add(q); } dupGs2.RTG[gs.currentFloor] = listThis.ToArray(); dupGs2.RTG[gs.currentFloor - 1] = listDown.ToArray(); if (CheckIfValid(dupGs2)) { iterate(dupGs2); } } } } return(maxIteration); }