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]; } }
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); }
public void RemoveWord(string word, WordSpace ws) { wordToSpaceMap.Remove(word); spaceToWordMap.Remove(ws.ToString()); wordsByLength[word.Length].Add(word); remainingSpaces[word.Length].Add(ws); }
public void WriteWordIntoSpace(string word, WordSpace ws, StringBuilder[] resultHolder) { if (ws.orientation == Orientation.Vertical) { WriteVerticalWord(word, ws, resultHolder); } else { WriteHorizontalWord(word, ws, resultHolder); } }
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); }
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); }
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]); }
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); }
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); }
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); }
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); }
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); }
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); }
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); } } }