コード例 #1
0
        private void Initialize(IList <FarmNode> rottenOranges)
        {
            // prepare the adjancy list for the rotten oranges.
            foreach (var item in rottenOranges)
            {
                // add left, right, top, bottom neighbour)
                if (IsValid(item.X - 1, item.Y))
                {
                    FarmNode leftNode = new FarmNode(item.X - 1, item.Y);
                    item.Neighbour.AddFirst(leftNode);
                }

                if (IsValid(item.X + 1, item.Y))
                {
                    FarmNode rightNode = new FarmNode(item.X + 1, item.Y);
                    item.Neighbour.AddFirst(rightNode);
                }

                if (IsValid(item.X, item.Y - 1))
                {
                    FarmNode topNode = new FarmNode(item.X, item.Y - 1);
                    item.Neighbour.AddFirst(topNode);
                }

                if (IsValid(item.X, item.Y + 1))
                {
                    FarmNode bottomNode = new FarmNode(item.X, item.Y + 1);
                    item.Neighbour.AddFirst(bottomNode);
                }
            }
        }
コード例 #2
0
        private IList <FarmNode> CollectRottenOranges(int[,] oranges)
        {
            List <FarmNode> farmNodes = new List <FarmNode>();

            for (int i = 0; i < oranges.GetLength(0); i++)
            {
                for (int j = 0; j < oranges.GetLength(1); j++)
                {
                    if (oranges[i, j] == 2)
                    {
                        FarmNode node = new FarmNode(i, j);
                        farmNodes.Add(node);
                    }
                }
            }

            return(farmNodes);
        }
コード例 #3
0
        private IList <FarmNode> GetAllNeighbours(FarmNode node)
        {
            List <FarmNode> neighbours = new List <FarmNode>();

            //neighbours.Add(node);

            // Get all bottom
            neighbours.AddRange(this.GetAllBottomNeighbours(node.X, node.Y));

            // get all top
            neighbours.AddRange(this.GetAllTopNeighbours(node.X, node.Y));

            // get all left
            neighbours.AddRange(this.GetAllLeftNeighbours(node.X, node.Y));

            // get all right
            neighbours.AddRange(this.GetAllRightNeighbours(node.X, node.Y));

            return(neighbours);
        }
コード例 #4
0
        private void TraverseFarm(FarmNode node, IList <FarmNode> visitedNodes)
        {
            Queue <FarmNode> queue = new Queue <FarmNode>();

            queue.Enqueue(node);

            do
            {
                FarmNode         tempNode   = queue.Dequeue();
                IList <FarmNode> neighbours = this.GetAllNeighbours(tempNode);

                foreach (var item in neighbours)
                {
                    if (!Exists(visitedNodes, item))
                    {
                        queue.Enqueue(item);
                        visitedNodes.Add(item);
                    }
                }
            }while (queue.Count > 0);
        }
コード例 #5
0
 private bool Predicate(FarmNode src, FarmNode target)
 {
     return(src.X == target.X && src.Y == target.Y);
 }
コード例 #6
0
 private bool Exists(IList <FarmNode> nodes, FarmNode node)
 {
     return(nodes.Any(item => Predicate(item, node)));
 }