Beispiel #1
0
        /// <summary>
        /// One simple algorithm
        /// </summary>
        /// <param name="inputObject"></param>
        /// <returns></returns>
        public OutputObject Execute(InputObject inputObject)
        {
            int minCell = 2 * inputObject.MinIngredient;

            // we want to find all rectangle whose area is between mincell and
            List <int[]> allRectangles = SlicingTools.GetAllRectangles(minCell, inputObject.MaxCell);

            // Execute algorithm
            return(SlicingAlgorithm.Execute(inputObject, allRectangles));
        }
        public static void Main(string[] args)
        {
            foreach (string file in inFiles)
            {
                Console.WriteLine(string.Format("Read file : {0}", file));

                InputObject  inputObject  = null;
                OutputObject outputObject = null;

                // Now we want to execute all known algorithm
                List <IAlgorithm> algorithms = new List <IAlgorithm>();
                algorithms.Add(new BasicAlgorithm());
                algorithms.Add(new SortAscendingAlgorithm());
                algorithms.Add(new SortDescendingAlgorithm());

                foreach (IAlgorithm algorithm in algorithms)
                {
                    string fileOutName = file.Split('\\').Last().Split('.').First() + ".out";
                    // Read file an read and put them inside c# object
                    using (var streamReader = new StreamReader(@file))
                    {
                        inputObject = new InputObject(streamReader);
                    }

                    Console.WriteLine(string.Format("Apply algorithm : {0} on this file", algorithm.GetName()));
                    outputObject = algorithm.Execute(inputObject);
                    Console.WriteLine(string.Format("{0} point with this algorithm", SlicingTools.GetNbPoint(outputObject)));

                    if (!Directory.Exists(outPath + algorithm.GetName()))
                    {
                        Directory.CreateDirectory(outPath + algorithm.GetName());
                    }
                    // Write file from c# object
                    using (var streamWriter = new StreamWriter(outPath + algorithm.GetName() + "\\" + fileOutName))
                    {
                        outputObject.Write(streamWriter);
                    }
                }
                Console.WriteLine();
            }
        }
Beispiel #3
0
        /// <summary>
        /// One possible algorithm to execute. It crosse pizza from top left to bottom right
        /// Result depend of the order of given rectangles
        /// </summary>
        /// <param name="inputObject"></param>
        /// <param name="rectangles"></param>
        /// <returns></returns>
        public static OutputObject Execute(InputObject inputObject, List <int[]> rectangles)
        {
            OutputObject result = new OutputObject();

            // now we cross pizza, we start at the left top
            for (int i = 0; i < inputObject.NbRow; i++)
            {
                for (int j = 0; j < inputObject.NbCol; j++)
                {
                    bool hasOneValidSlice = false;
                    foreach (int[] rectangle in rectangles)
                    {
                        if (SlicingTools.IsValidSlice(i, j, rectangle, inputObject) && !hasOneValidSlice)
                        {
                            hasOneValidSlice = true;
                            for (int x = i; x < i + rectangle[0]; x++)
                            {
                                for (int y = j; y < j + rectangle[1]; y++)
                                {
                                    inputObject.Pizza[x][y] = 'O';
                                }
                            }
                            result.NbSlices++;
                            int[] coordonne = { i, j, i + rectangle[0] - 1, j + rectangle[1] - 1 };
                            result.Slices.Add(coordonne);
                        }
                    }
                    if (!hasOneValidSlice)
                    {
                        inputObject.Pizza[i][j] = 'X';
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// determine if a slice is a valid one !
        /// </summary>
        /// <param name="originAbsicsse"></param>
        /// <param name="origineOrdonne"></param>
        /// <param name="rectangle"></param>
        /// <returns></returns>
        public static bool IsValidSlice(int originAbsicsse, int origineOrdonne, int[] rectangle, InputObject inputObject)
        {
            bool valid    = true;
            int  nbTomate = 0;
            int  nbMush   = 0;

            for (int i = originAbsicsse; i < originAbsicsse + rectangle[0]; i++)
            {
                for (int j = origineOrdonne; j < origineOrdonne + rectangle[1]; j++)
                {
                    if (i >= inputObject.NbRow || j >= inputObject.NbCol || (inputObject.Pizza[i][j] != 'T' && inputObject.Pizza[i][j] != 'M'))
                    {
                        valid = false;
                    }
                    else if (inputObject.Pizza[i][j] == 'M')
                    {
                        nbMush++;
                    }
                    else if (inputObject.Pizza[i][j] == 'T')
                    {
                        nbTomate++;
                    }
                }
            }

            if (nbMush < inputObject.MinIngredient || nbTomate < inputObject.MinIngredient)
            {
                valid = false;
            }

            return(valid);
        }