Example #1
0
        public static IEnumerable <Method.Path> Process(double[][] matrix)
        {
            var obj  = new TSPSolver();
            var step = obj.solve(matrix.Length, cloneFull(matrix));

            if (obj.route.Count <= 0)
            {
                yield break;
            }
            var first = obj.route.Keys.Min();
            var prev  = first;
            var next  = 0;
            var found = false;

            while ((next = obj.getNextRoute(prev, out found)) != first && found)
            {
                yield return(new Method.Path {
                    From = prev, To = next, Distance = (int)matrix[prev][next]
                });

                prev = next;
            }
            if (found)
            {
                yield return new Method.Path {
                           From = prev, To = next, Distance = (int)matrix[prev][next]
                }
            }
            ;
        }
Example #2
0
        public static IEnumerable <Method.Path> Process(string[] screen, World world0)
        {
            var dict = new Dictionary <Tuple <int, int>, int>();

            for (var y = 0; y < screen.Length; y++)
            {
                for (var x = 0; x < screen[y].Length; x++)
                {
                    if (screen[y][x] == '*')
                    {
                        dict[Tuple.Create(y, x)] = dict.Count;
                    }
                }
            }
            var matrix = Enumerable.Range(0, dict.Count)
                         .Select(_ => Enumerable.Range(0, dict.Count).Select(__ => double.PositiveInfinity).ToArray())
                         .ToArray();
            var isolated = new HashSet <int>();

            foreach (var item in dict.ToArray())
            {
                var src  = item.Value;
                var wave = new WaveOld(world0.Clone(), "*", ":*", null);
                wave.Update(item.Key.Item1, item.Key.Item2, true, 0);
                foreach (var scored in wave.Scored)
                {
                    var coord = Tuple.Create(scored.Y, scored.X);
                    if (dict.ContainsKey(coord))
                    {
                        var dst = dict[coord];
                        var val = scored.StepsCount < 20 ? 20 : 10000;
                        matrix[src][dst] = val;
                    }
                }
                if (wave.Scored.Length == 0)
                {
                    dict.Remove(item.Key);
                    isolated.Add(src);
                }
            }
            if (isolated.Count != 0)
            {
                matrix = matrix.Where((x, i) => !isolated.Contains(i))
                         .Select(z => z.Where((x, i) => !isolated.Contains(i)).ToArray())
                         .ToArray();
            }

            //for (var i = 0; i < matrix.Length; i++)
            //    System.Diagnostics.Trace.WriteLine("[" + i + "] " + string.Join(" ", matrix[i]));

            var obj   = new TSPSolver();
            var step  = obj.solve(dict.Count, cloneFull(matrix));
            var first = obj.route.Keys.Min();
            var prev  = first;
            var next  = 0;
            var found = false;

            while ((next = obj.getNextRoute(prev, out found)) != first && found)
            {
                yield return(new Method.Path {
                    From = prev, To = next, Distance = (int)matrix[prev][next]
                });

                prev = next;
            }
            if (found)
            {
                yield return new Method.Path {
                           From = prev, To = next, Distance = (int)matrix[prev][next]
                }
            }
            ;
        }