public static int Score(Solution solution, PictureDescription pd)
 {
     if(!Validate(solution, pd))
     {
         return 0;
     }
     return 0;
 }
        public Form1(PictureDescription pic)
        {
            InitializeComponent();

            picturePrinter1.pic = pic;

            picturePrinter1.Invalidate();
        }
        private static void TryHorizontalLines(PictureDescription descriptor, Solution solution, ref Command? bestCommand, ref int bestScore)
        {
            for (int y = 0; y < descriptor.NumRows; ++y)
            {
                for (int x = 0; x < descriptor.NumCols;)
                {
                    int startX = x;
                    int endX = x;
                    bool skipped = false;
                    int score = 0;
                    
                    for (;;)
                    {
                        if (x >= descriptor.NumCols)
                        {
                            break;
                        }
                        if (descriptor.IsFilled(x, y))
                        {
                            if (!solution.IsFilled(x, y))
                            {
                                ++score;
                                endX = x;
                            }
                        } else if (skipped)
                        {
                            skipped = true;
                        } else
                        {
                            ++x;
                            break;
                        }

                        ++x;
                    }

                    int len = endX - startX + 1;

                    if (len > 0)
                    {
                        if (score > 0 && score > bestScore)
                        {
                            var command = solution.MakeHorizontalLine(startX, y, len);

                            if (score > bestScore)
                            {
                                bestCommand = command;
                                bestScore = score;
                            }
                        }
                    }
                }
            }
        }
        public static Solution solve(PictureDescription pd)
        {
            Solution solution = new Solution(pd);
            //PictureDescription auxPd = new PictureDescription(pd.NumRows, pd.NumCols);
            for (int row = 0; row < pd.NumRows; row++)
            {
                DrawState currentState = DrawState.EMPTY;
                int[] from = new int[2];
                int length = 0;

                for (int column = 0; column < pd.NumCols; column++)
                {
                    switch (currentState)
                    {
                        case DrawState.EMPTY:
                            if (pd.IsFilled(column, row))
                            {
                                from[0] = column;
                                from[1] = row;
                                length = 1;
                                currentState = DrawState.FILL;
                                if (column == pd.NumCols - 1)
                                {
                                    solution.AddCommand(solution.MakeHorizontalLine(from[0], from[1], length));
                                    currentState = DrawState.EMPTY;
                                }
                            }

                            break;
                        case DrawState.FILL:
                            if (!pd.IsFilled(column, row))
                            {
                                solution.AddCommand(solution.MakeHorizontalLine(from[0], from[1], length));
                                currentState = DrawState.EMPTY;
                            }else if (column == pd.NumCols - 1)
                            {
                                solution.AddCommand(solution.MakeHorizontalLine(from[0], from[1], ++length));
                                currentState = DrawState.EMPTY;
                            }
                            else
                            {
                                length++;
                            }
                            break;
                    }
                }
            }

            return solution;
        }
 public static bool Validate(Solution solution, PictureDescription pd)
 {
     for (int row = 0; row < pd.NumRows; row++)
     {
         for (int column = 0; column < pd.NumCols; column++)
         {
             if(solution.IsFilled(column, row) != pd.IsFilled(column, row))
             {
                 Console.WriteLine(row + " " + column);
                 return false;
             }
         }
     }
     return true;
 }
        public static HashMapGrid hashmapHorizontalLines(PictureDescription grid)
        {
            HashMapGrid gridToReturn = new HashMapGrid(grid.NumRows, grid.NumCols);

            for (int y = 0; y < grid.NumRows; y++)
            {
                for (int x = 0; x < grid.NumCols; x++)
                {
                    if (!grid.IsFilled(x, y))
                    {
                        gridToReturn.SetValue(x, y, 0);
                    }
                    else
                    {

                        int count = 1;

                        for (int xx = x + 1; xx < grid.NumCols; xx++)
                        {
                            if (grid.IsFilled(xx, y))
                            {
                                count++;
                            }
                            else
                            {
                                break;
                            }
                        }

                        for (int xx = x - 1; xx >= 0; xx--)
                        {
                            if (grid.IsFilled(xx, y))
                            {
                                count++;
                            }
                            else
                            {
                                break;
                            }
                        }

                        gridToReturn.SetValue(x, y, count);
                    }
                }
            }
            return gridToReturn;
        }
        public static HashMapGrid hashmapSquares(PictureDescription grid)
        {
            HashMapGrid gridToReturn = new HashMapGrid(grid.NumRows, grid.NumCols);

            for (int y = 0; y < grid.NumRows; y++)
            {
                for (int x = 0; x < grid.NumCols; x++)
                {
                    if (!grid.IsFilled(x, y))
                    {
                        gridToReturn.SetValue(x, y, 0);
                    }
                    else
                    {
                        gridToReturn.SetValue(x, y, 0);
                        for (int width = 1; x + width < grid.NumCols && x - width >= 0
                            && y + width < grid.NumRows && y - width >= 0; width++)
                        {
                            Boolean valid = true;
                            for (int xx = x - width; xx <= x + width; xx++)
                            {
                                for (int yy = y - width; yy <= y + width; y++)
                                {
                                    if (!grid.IsFilled(xx, yy))
                                    {
                                        valid = false;
                                        goto terminate;
                                    }
                                }
                            }
                            terminate:
                            if (valid)
                            {
                                gridToReturn.SetValue(x, y, width);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return gridToReturn;
        }
        public static Solution Solve(PictureDescription descriptor)
        {
            var solution = new Solution(descriptor);

            while (!solution.Covers(descriptor))
            {
                int bestScore = int.MinValue;
                Command? bestCommand = null;

                TryHorizontalLines(descriptor, solution, ref bestCommand, ref bestScore);

                solution.AddCommand(bestCommand.Value);
            }

            solution.AddClearCommands(descriptor);

            return solution;
        }
        public static PictureDescription ReadFromFile(string filename)
        {
            using (var reader = File.OpenText(filename))
            {
                string descLine = reader.ReadLine();

                var match = Regex.Match(descLine, "^([0-9]+) ([0-9]+)$");
                int nRows = int.Parse(match.Groups[1].Value);
                int nCols = int.Parse(match.Groups[2].Value);

                var desc = new PictureDescription(nRows, nCols);

                for (int i = 0; i < nRows; ++i)
                {
                    var line = reader.ReadLine();

                    for (int j = 0; j < nCols; ++j)
                    {
                        char ch = line[j];

                        if (ch == '.')
                        {
                            // Do nothing
                        } else if (ch == '#')
                        {
                            desc.Fill(j, i);
                        } else
                        {
                            throw new NotImplementedException();
                        }
                    }
                }

                return desc;
            }
        }
 public Solution(PictureDescription desc)
 {
     nRows = desc.NumRows;
     nCols = desc.NumCols;
 }
 public void AddClearCommands(PictureDescription desc)
 {
     for (int y = 0; y < desc.NumRows; ++y)
     {
         for (int x = 0; x < desc.NumCols; ++x)
         {
             if (!desc.IsFilled(x, y) && IsFilled(x, y))
             {
                 AddCommand(MakeClear(x, y));
             }
         }
     }
 }
        public bool Covers(PictureDescription desc)
        {
            for (int y = 0; y < desc.NumRows; ++y)
            {
                for (int x = 0; x < desc.NumCols; ++x)
                {
                    if (desc.IsFilled(x, y) && !IsFilled(x, y))
                    {
                        return false;
                    }
                }
            }

            return true;
        }