public void NeighborsFinder_Dim_8(int dimention)
        {
            //  0(-)| 1(+)| 2(-)| 3(+)| 4(-)| 5(+)| 6(-)| 7(+)
            //  8(+)| 9(-)|10(+)|11(-)|12(+)|13(-)|14(+)|15(-)
            // 16(-)|17(+)|18(-)|19(+)|20(-)|21(+)|22(-)|23(+)
            // 24(+)|25(-)|26(+)|27(-)|28(+)|29(-)|30(+)|31(-)
            // 32(-)|33(+)|34(-)|35(+)|36(-)|37(+)|38(-)|39(+)
            // 40(+)|41(-)|42(+)|43(-)|44(+)|45(-)|46(+)|47(-)
            // 48(-)|49(+)|50(-)|51(+)|52(-)|53(+)|54(-)|55(+)
            // 56(+)|57(-)|58(+)|59(-)|60(+)|61(-)|62(+)|63(-)

            var finder = new NeighborsFinder(dimention);

            // Zero row
            Assert.AreEqual(finder[0], new CellNeighbors(-1, -1, -1, 9));
            Assert.AreEqual(finder[7], new CellNeighbors(-1, 14, -1, -1));

            // First row
            Assert.AreEqual(finder[9], new CellNeighbors(0, 16, 2, 18));
            Assert.AreEqual(finder[14], new CellNeighbors(5, 21, 7, 23));

            // Second row
            Assert.AreEqual(finder[18], new CellNeighbors(9, 25, 11, 27));
            Assert.AreEqual(finder[21], new CellNeighbors(12, 28, 14, 30));

            // Third row
            Assert.AreEqual(finder[27], new CellNeighbors(18, 34, 20, 36));
            Assert.AreEqual(finder[28], new CellNeighbors(19, 35, 21, 37));

            // Third row
            Assert.AreEqual(finder[3, 3], new CellNeighbors(18, 34, 20, 36));
            Assert.AreEqual(finder[3, 4], new CellNeighbors(19, 35, 21, 37));
        }
        public void NeighborsFinder_IsNeighbors(int dimention)
        {
            //  0(-)| 1(+)| 2(-)| 3(+)| 4(-)| 5(+)| 6(-)| 7(+)
            //  8(+)| 9(-)|10(+)|11(-)|12(+)|13(-)|14(+)|15(-)
            // 16(-)|17(+)|18(-)|19(+)|20(-)|21(+)|22(-)|23(+)
            // 24(+)|25(-)|26(+)|27(-)|28(+)|29(-)|30(+)|31(-)
            // 32(-)|33(+)|34(-)|35(+)|36(-)|37(+)|38(-)|39(+)
            // 40(+)|41(-)|42(+)|43(-)|44(+)|45(-)|46(+)|47(-)
            // 48(-)|49(+)|50(-)|51(+)|52(-)|53(+)|54(-)|55(+)
            // 56(+)|57(-)|58(+)|59(-)|60(+)|61(-)|62(+)|63(-)

            var finder = new NeighborsFinder(dimention);

            Assert.IsFalse(finder.AreNeighbors(8, 7));
            Assert.IsFalse(finder.AreNeighbors(0, 0));
            Assert.IsFalse(finder.AreNeighbors(24, 8));
            Assert.IsFalse(finder.AreNeighbors(-1, 0));
            Assert.IsFalse(finder.AreNeighbors(0, -1));
            Assert.IsFalse(finder.AreNeighbors(-1, -1));

            Assert.IsTrue(finder.AreNeighbors(35, 26));
            Assert.IsTrue(finder.AreNeighbors(35, 28));
            Assert.IsTrue(finder.AreNeighbors(35, 42));
            Assert.IsTrue(finder.AreNeighbors(35, 44));

            Assert.IsFalse(finder.AreNeighbors(35, 27));
            Assert.IsFalse(finder.AreNeighbors(35, 34));
            Assert.IsFalse(finder.AreNeighbors(35, 36));
            Assert.IsFalse(finder.AreNeighbors(35, 43));
        }
        public void NeighborsFinder_GetSmthCell(int dimention)
        {
            //  0(-)| 1(+)| 2(-)| 3(+)| 4(-)| 5(+)| 6(-)| 7(+)
            //  8(+)| 9(-)|10(+)|11(-)|12(+)|13(-)|14(+)|15(-)
            // 16(-)|17(+)|18(-)|19(+)|20(-)|21(+)|22(-)|23(+)
            // 24(+)|25(-)|26(+)|27(-)|28(+)|29(-)|30(+)|31(-)
            // 32(-)|33(+)|34(-)|35(+)|36(-)|37(+)|38(-)|39(+)
            // 40(+)|41(-)|42(+)|43(-)|44(+)|45(-)|46(+)|47(-)
            // 48(-)|49(+)|50(-)|51(+)|52(-)|53(+)|54(-)|55(+)
            // 56(+)|57(-)|58(+)|59(-)|60(+)|61(-)|62(+)|63(-)

            var finder = new NeighborsFinder(dimention);

            // default deep
            Assert.AreEqual(finder.GetLeftTopCell(26), 17);
            Assert.AreEqual(finder.GetLeftBotCell(26), 33);
            Assert.AreEqual(finder.GetRightTopCell(26), 19);
            Assert.AreEqual(finder.GetRightBotCell(26), 35);

            // deep = -1
            Assert.AreEqual(finder.GetLeftTopCell(26, -1), -1);

            // deep = 2
            Assert.AreEqual(finder.GetLeftTopCell(26, 2), 8);
            Assert.AreEqual(finder.GetLeftBotCell(26, 2), 40);
            Assert.AreEqual(finder.GetRightTopCell(26, 2), 12);
            Assert.AreEqual(finder.GetRightBotCell(26, 2), 44);

            // deep = 3
            Assert.AreEqual(finder.GetLeftTopCell(26, 3), -1);
            Assert.AreEqual(finder.GetLeftBotCell(26, 3), -1);
            Assert.AreEqual(finder.GetRightTopCell(26, 3), 5);
            Assert.AreEqual(finder.GetRightBotCell(26, 3), 53);
        }
        public void NeighborsFinder_Dim_2(int dimention)
        {
            // 0(-)|1(+)
            // 2(+)|3(-)

            var finder = new NeighborsFinder(dimention);

            // Zero row
            Assert.AreEqual(finder[0], new CellNeighbors(-1, -1, -1, 3));
            Assert.AreEqual(finder[1], new CellNeighbors(-1, 2, -1, -1));

            // First row
            Assert.AreEqual(finder[2], new CellNeighbors(-1, -1, 1, -1));
            Assert.AreEqual(finder[3], new CellNeighbors(0, -1, -1, -1));
        }
Ejemplo n.º 5
0
        public void Find2Neighbors()
        {
            var map = new byte[3, 3] {
                { 1, 2, 3, },
                { 3, 4, 2, },
                { 5, 6, 5, },
            };

            var pointX = 2;
            var pointY = 2;

            var neighbors = NeighborsFinder.Find(map, pointX, pointY);

            Assert.Equal(2, neighbors.Length);
            Assert.Contains(neighbors, n => n.X == pointX - 1 && n.Y == pointY);
            Assert.Contains(neighbors, n => n.X == pointX && n.Y == pointY - 1);
        }
        public void NeighborsFinder_Dim_3(int dimention)
        {
            // 0(-)|1(+)|2(-)
            // 3(+)|4(-)|5(+)
            // 6(-)|7(+)|8(-)

            var finder = new NeighborsFinder(dimention);

            // Zero row
            Assert.AreEqual(finder[1], new CellNeighbors(-1, 3, -1, 5));

            // First row
            Assert.AreEqual(finder[3], new CellNeighbors(-1, -1, 1, 7));
            Assert.AreEqual(finder[4], new CellNeighbors(0, 6, 2, 8));

            // Second row
            Assert.AreEqual(finder[7], new CellNeighbors(3, -1, 5, -1));
        }
        public void NeighborsFinder_Dimension_1(int dimention)
        {
            var finder = new NeighborsFinder(dimention);

            Assert.AreEqual(finder[0], new CellNeighbors(-1, -1, -1, -1));
        }
Ejemplo n.º 8
0
        public Location[] Find(byte[,] map, Location start, Location finish)
        {
            if (start == finish)
            {
                throw new ArgumentException("Start and finish can't be the same");
            }

            if (start.X < 0 || start.X >= map.GetLength(0) ||
                start.Y < 0 || start.Y >= map.GetLength(1))
            {
                throw new ArgumentException(nameof(start));
            }

            if (finish.X < 0 || finish.X >= map.GetLength(0) ||
                finish.Y < 0 || finish.Y >= map.GetLength(1))
            {
                throw new ArgumentException(nameof(finish));
            }

            var costMap = new ushort[map.GetLength(0), map.GetLength(1)];

            costMap[start.X, start.Y] = MaxPossibilitiesValue;

            bool isFinish = false;

            var frontier = new SimplePriorityQueue();

            frontier.Insert(start, 0);

            var comeFrom = new Dictionary <(int x, int y), Location>();

            var cheapestPath = int.MaxValue;

            var iterationsCount = 0;

            while (!isFinish)
            {
                var currentCell = frontier.Get();

                var pathCost = costMap[currentCell.Cell.X, currentCell.Cell.Y];

                // if cheapest path from frontier cost more,
                // than already finded cheapest way to finish -
                // no more reason to continue calculating.
                if (pathCost > cheapestPath)
                {
                    isFinish = true;
                }

                var neighbors = NeighborsFinder.Find(map, currentCell.Cell.X, currentCell.Cell.Y);

                foreach (var neighbor in neighbors)
                {
                    var passability = map[neighbor.X, neighbor.Y];
                    if (passability == 0)
                    {
                        continue;
                    }

                    var neighborCost           = MaxPossibilitiesValue - passability;
                    var cheapestCostOnNeighbor = costMap[neighbor.X, neighbor.Y];

                    var currentPathCost = (ushort)(pathCost + neighborCost);

                    if (cheapestCostOnNeighbor == 0 || currentPathCost < cheapestCostOnNeighbor)
                    {
                        costMap[neighbor.X, neighbor.Y]    = currentPathCost;
                        comeFrom[(neighbor.X, neighbor.Y)] = currentCell.Cell;