Esempio n. 1
0
        public int TimeToRotOranges(int[,] mat)
        {
            #region add rotten in Queue and Delimeter
            for (int i = 0; i < ROW; i++)
            {
                for (int j = 0; j < COL; j++)
                {
                    if (mat[i, j] == 2)
                    {
                        q.Enqueue(new Coordinate(i, j));
                    }
                }
            }

            q.Enqueue(new Coordinate(-1, -1));

            #endregion


            var flag = false;

            while (q.Count() != 0)
            {
                while (!IfPeekDelimiter(q.Peek()))
                {
                    Coordinate element = q.Dequeue();

                    if (IsFreshOrange(mat, element.X - 1, element.Y))
                    {
                        mat[element.X - 1, element.Y] = 2;
                        q.Enqueue(new Coordinate(element.X - 1, element.Y));

                        if (!flag)
                        {
                            flag = true;
                            _count++;
                        }
                    }

                    if (IsFreshOrange(mat, element.X, element.Y + 1))
                    {
                        mat[element.X, element.Y + 1] = 2;
                        q.Enqueue(new Coordinate(element.X, element.Y + 1));

                        if (!flag)
                        {
                            flag = true;
                            _count++;
                        }
                    }

                    if (IsFreshOrange(mat, element.X + 1, element.Y))
                    {
                        mat[element.X + 1, element.Y] = 2;
                        q.Enqueue(new Coordinate(element.X + 1, element.Y));
                        if (!flag)
                        {
                            flag = true;
                            _count++;
                        }
                    }

                    if (IsFreshOrange(mat, element.X, element.Y - 1))
                    {
                        mat[element.X, element.Y - 1] = 2;
                        q.Enqueue(new Coordinate(element.X, element.Y - 1));
                        if (!flag)
                        {
                            flag = true;
                            _count++;
                        }
                    }
                }

                q.Dequeue();
                flag = false;
                if (q.Count() != 0)
                {
                    q.Enqueue(new Coordinate(-1, -1));
                }
            }

            return(IfAnyFreshOrangesLeft(mat) ? -1 : _count);
        }
Esempio n. 2
0
 public bool IfPeekDelimiter(Coordinate c)
 {
     return(c.X == -1 && c.Y == -1);
 }