public void Overlay() { for (int i = 0; i < Constants.Width; i++) { for (int j = 0; j < Constants.Length; j++) { var coordinate = new TestCoordinate(i, j); Assign(graph[coordinate], matrix[i, j]); } } }
public KeyValuePair <string, int> GetDistance(Dictionary <char, Coordinate> d, bool[,] TheGrid) { int Distance = 0; List <char> AllTheKeys = new List <char>(); //setting up a key foreach (KeyValuePair <char, Coordinate> c in d) { AllTheKeys.Add(c.Key); } AllTheKeys.Sort(); string Keystring = ""; foreach (char c in AllTheKeys) { Keystring += c; } //doing the path int MaxX = TheGrid.GetLength(0); int MaxY = TheGrid.GetLength(1); Coordinate TestCoordinate; List <Coordinate> AllAdjantDirections = new List <Coordinate> { { new Coordinate(0, 1) }, { new Coordinate(0, -1) }, { new Coordinate(-1, 0) }, { new Coordinate(1, 0) } }; List <PathFinder> Paths = new List <PathFinder>(); Paths.Add(new PathFinder(d)); List <PathFinder> NextPaths = new List <PathFinder>(); HashSet <string> PathDigests = new HashSet <string>(); bool IsCrossing = true; int DirectionCounter; PathFinder Digesting; bool GetOut = false; while (!GetOut) { Distance++; foreach (PathFinder p in Paths) { if (p.TargetFound()) { GetOut = true; break; } DirectionCounter = 0; foreach (Coordinate ad in AllAdjantDirections) { TestCoordinate = p.GetCurrentPosition().GetSum(ad); if (!TheGrid[TestCoordinate.x, TestCoordinate.y]) { DirectionCounter++; } IsCrossing = DirectionCounter > 2; } foreach (Coordinate ad in AllAdjantDirections) { TestCoordinate = p.GetCurrentPosition().GetSum(ad); if (TestCoordinate.IsInPositiveBounds(MaxX - 1, MaxY - 1) & !TheGrid[TestCoordinate.x, TestCoordinate.y] & !p.HasVisited(TestCoordinate)) { Digesting = new PathFinder(TestCoordinate, p.getVisitedPositions(), p.getTargets()); if (!PathDigests.Contains(Digesting.MakePathDigest())) { NextPaths.Add(new PathFinder(TestCoordinate, p.getVisitedPositions(), p.getTargets())); if (IsCrossing) { PathDigests.Add(Digesting.MakePathDigest()); } } } } } Paths = new List <PathFinder>(NextPaths); NextPaths.Clear(); } Distance--; return(new KeyValuePair <string, int>(Keystring, Distance)); }
public string Result() { int Sum = 0; int Sum2 = 0; Dictionary <int, Coordinate> CurrentPosition = new Dictionary <int, Coordinate>(); CurrentPosition.Add(1, new Coordinate(1, 1)); Coordinate LastPosition = new Coordinate(1, 1); Coordinate TargetPosition = new Coordinate(31, 39); List <Coordinate> AllAdjantDirections = new List <Coordinate>(); AllAdjantDirections.Add(new Coordinate(1, 0)); AllAdjantDirections.Add(new Coordinate(0, 1)); AllAdjantDirections.Add(new Coordinate(0, -1)); AllAdjantDirections.Add(new Coordinate(-1, 0)); Coordinate TestCoordinate; int NextInt = 1; bool Quit = false; int StepCounter = 0; while (!Quit) { StepCounter++; Sum++; Dictionary <int, Coordinate> IteratingList = new Dictionary <int, Coordinate>(CurrentPosition); foreach (KeyValuePair <int, Coordinate> c in IteratingList) { foreach (Coordinate d in AllAdjantDirections) { TestCoordinate = c.Value.GetSum(d); if (TestCoordinate.IsOn(TargetPosition)) { Quit = true; } //is this side a valid square? if (TestCoordinate.x >= 0 && TestCoordinate.x < 50 && TestCoordinate.y >= 0 && TestCoordinate.y < 50 && !TheGrid[TestCoordinate.x, TestCoordinate.y]) { if (StepCounter <= 50) { Sum2++; } NextInt++; CurrentPosition.Add(NextInt, TestCoordinate); TheGrid[TestCoordinate.x, TestCoordinate.y] = true; } } CurrentPosition.Remove(c.Key); } } //This is just some visuals StringBuilder sBuilder = new StringBuilder(); for (int x = 0; x < 50; x++) { sBuilder.Append("\r\n"); for (int y = 0; y < 50; y++) { if (TheGrid[y, x] == true) { sBuilder.Append("#"); } else { sBuilder.Append("."); } } } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; return("Del 1: " + Sum + " och del 2: " + Sum2 + " Executed in " + ts.TotalMilliseconds.ToString() + " ms" + "\r\n" + sBuilder.ToString()); }