Esempio n. 1
0
 public void WriteHorizontalWord(string word, WordSpace ws, StringBuilder[] resultHolder)
 {
     for (int i = 0; i < word.Length; i++)
     {
         resultHolder[ws.start.row][ws.start.col + i] = word[i];
     }
 }
Esempio n. 2
0
        private static WordSpace GetStartWordSpace(string[] rows, Dictionary <int, HashSet <int> > remainingPoints)
        {
            for (int row = 0; row < rows.Length; row++)
            {
                for (int col = 0; col < rows[row].Length; col++)
                {
                    if (!remainingPoints.ContainsKey(row))
                    {
                        break;
                    }

                    if (!remainingPoints[row].Contains(col))
                    {
                        continue;
                    }

                    RemoveFromRemainingPoints(remainingPoints, row, col);

                    char c = rows[row][col];
                    if (c == '-')
                    {
                        WordSpace ws = GetHorizontalWordspace(row, col, rows);
                        if (ws == null)
                        {
                            ws = GetVerticalWordspace(row, col, rows);
                        }

                        return(ws);
                    }
                }
            }

            return(null);
        }
Esempio n. 3
0
 public void RemoveWord(string word, WordSpace ws)
 {
     wordToSpaceMap.Remove(word);
     spaceToWordMap.Remove(ws.ToString());
     wordsByLength[word.Length].Add(word);
     remainingSpaces[word.Length].Add(ws);
 }
Esempio n. 4
0
 public void WriteWordIntoSpace(string word, WordSpace ws, StringBuilder[] resultHolder)
 {
     if (ws.orientation == Orientation.Vertical)
     {
         WriteVerticalWord(word, ws, resultHolder);
     }
     else
     {
         WriteHorizontalWord(word, ws, resultHolder);
     }
 }
Esempio n. 5
0
            public bool PlaceWord(string word, WordSpace ws)
            {
                if (IsConflict(word, ws))
                {
                    return(false);
                }

                wordToSpaceMap[word]          = ws;
                spaceToWordMap[ws.ToString()] = word;
                wordsByLength[word.Length].Remove(word);
                remainingSpaces[word.Length].Remove(ws);
                return(true);
            }
Esempio n. 6
0
        private static List <WordSpaceIntersection> GetHorizontalIntersections(
            WordSpace ws, string[] rows)
        {
            List <WordSpaceIntersection> result = new List <WordSpaceIntersection>();

            for (int i = 0; i < ws.length; i++)
            {
                if (IsHorizontalIntersection(ws.start.row + i, ws.start.col, rows))
                {
                    result.Add(GetHorizontalIntersection(ws.start.row + i, ws.start.col, rows));
                }
            }
            return(result);
        }
Esempio n. 7
0
            private bool IsConflict(
                string word1, WordSpace ws1, string word2, WordSpaceIntersection wsi)
            {
                int indexOfIntersectionInWord1;
                int indexOfIntersectionInWord2;

                if (ws1.orientation == Orientation.Vertical)
                {
                    indexOfIntersectionInWord1 = wsi.ip.row - ws1.start.row;
                    indexOfIntersectionInWord2 = wsi.ip.col - wsi.ws.start.col;
                }
                else
                {
                    indexOfIntersectionInWord1 = wsi.ip.col - ws1.start.col;
                    indexOfIntersectionInWord2 = wsi.ip.row - wsi.ws.start.row;
                }

                return(word1[indexOfIntersectionInWord1] != word2[indexOfIntersectionInWord2]);
            }
Esempio n. 8
0
            private bool IsConflict(string word, WordSpace ws)
            {
                foreach (WordSpaceIntersection wsi in ws.intersections)
                {
                    string spaceKey = wsi.ws.ToString();
                    if (!spaceToWordMap.ContainsKey(spaceKey))
                    {
                        continue;
                    }

                    string intersectingWord = spaceToWordMap[spaceKey];
                    if (IsConflict(word, ws, intersectingWord, wsi))
                    {
                        return(true);
                    }
                }

                return(false);
            }
Esempio n. 9
0
        private static List <WordSpaceIntersection> GetIntersectingWordSpaces(
            WordSpace ws, string[] rows)
        {
            if (ws.intersections != null)
            {
                return(ws.intersections);
            }

            if (ws.orientation == Orientation.Horizontal)
            {
                ws.intersections = GetVerticalIntersections(ws, rows);
            }
            else
            {
                ws.intersections = GetHorizontalIntersections(ws, rows);
            }

            return(ws.intersections);
        }
Esempio n. 10
0
        private static Dictionary <int, List <WordSpace> > GetSpaces(string[] rows)
        {
            Dictionary <int, List <WordSpace> > wsbl            = new Dictionary <int, List <WordSpace> >();
            Dictionary <int, HashSet <int> >    remainingPoints = GetAllPoints(rows);

            while (remainingPoints.Count > 0)
            {
                WordSpace firstWordSpace = GetStartWordSpace(rows, remainingPoints);
                if (firstWordSpace == null)
                {
                    break;
                }

                HashSet <string> discovered = new HashSet <string>();
                discovered.Add(firstWordSpace.ToString());
                Queue <WordSpace> toExplore = new Queue <WordSpace>();
                toExplore.Enqueue(firstWordSpace);
                while (toExplore.Count > 0)
                {
                    WordSpace cws = toExplore.Dequeue();
                    RemoveFromRemainingPoints(remainingPoints, cws);

                    if (!wsbl.ContainsKey(cws.length))
                    {
                        wsbl[cws.length] = new List <WordSpace>();
                    }
                    wsbl[cws.length].Add(cws);

                    foreach (WordSpaceIntersection wsi in GetIntersectingWordSpaces(cws, rows))
                    {
                        string wsKey = wsi.ws.ToString();
                        if (!discovered.Contains(wsKey))
                        {
                            discovered.Add(wsKey);
                            toExplore.Enqueue(wsi.ws);
                        }
                    }
                }
            }

            return(wsbl);
        }
Esempio n. 11
0
        private static WordSpace GetVerticalWordspace(int row, int col, string[] rows)
        {
            int len = 0;
            int r   = row;

            while (rows[r][col] == '-' && r < rows.Length)
            {
                r++;
                len++;
            }

            if (len == 1)
            {
                return(null);
            }

            WordSpace ws = new WordSpace();

            ws.start       = new Point(row, col);
            ws.length      = len;
            ws.orientation = Orientation.Vertical;
            return(ws);
        }
Esempio n. 12
0
        private static WordSpace GetHorizontalWordspace(int row, int col, string[] rows)
        {
            int len = 0;
            int c   = col;

            while (c < rows[row].Length && rows[row][c] == '-')
            {
                c++;
                len++;
            }

            if (len == 1)
            {
                return(null);
            }

            WordSpace ws = new WordSpace();

            ws.start       = new Point(row, col);
            ws.length      = len;
            ws.orientation = Orientation.Horizontal;
            return(ws);
        }
Esempio n. 13
0
        private static WordSpaceIntersection GetVerticalIntersection(
            int row, int col, string[] rows)
        {
            int start = row;

            while (start >= 0 && rows[start][col] == '-')
            {
                start--;
            }
            if (start < 0 || rows[start][col] != '-')
            {
                start++;
            }
            int end = row;

            while (end < rows.Length && rows[end][col] == '-')
            {
                end++;
            }
            if (end == rows.Length || rows[end][col] != '-')
            {
                end--;
            }

            WordSpaceIntersection wsi = new WordSpaceIntersection();

            WordSpace ws = new WordSpace();

            ws.start       = new Point(start, col);
            ws.length      = (end - start) + 1;
            ws.orientation = Orientation.Vertical;
            wsi.ws         = ws;

            wsi.ip = new Point(row, col);

            return(wsi);
        }
Esempio n. 14
0
 private static void RemoveFromRemainingPoints(Dictionary <int, HashSet <int> > remainingPoints, WordSpace ws)
 {
     if (ws.orientation == Orientation.Horizontal)
     {
         for (int i = 0; i < ws.length; i++)
         {
             RemoveFromRemainingPoints(remainingPoints, ws.start.row, ws.start.col + i);
         }
     }
     else             // ws.orientation == Orientation.Vertical
     {
         for (int i = 0; i < ws.length; i++)
         {
             RemoveFromRemainingPoints(remainingPoints, ws.start.row + i, ws.start.col);
         }
     }
 }