Created
November 28, 2024 09:06
-
-
Save patriksima/4fa5085324d9715ee6f0f0c312530a9a to your computer and use it in GitHub Desktop.
Osmisměrka
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WordSearchSolver | |
{ | |
private static readonly (int, int)[] Directions = | |
[ | |
(0, 1), (1, 0), (1, 1), (-1, 0), (0, -1), (-1, -1), (1, -1), (-1, 1) | |
]; | |
private static List<List<(int, int)>> SearchWord(char[][] matrix, string word) | |
{ | |
var rows = matrix.Length; | |
var cols = matrix[0].Length; | |
var allResults = new List<List<(int, int)>>(); | |
for (var i = 0; i < rows; i++) | |
{ | |
for (var j = 0; j < cols; j++) | |
{ | |
if (matrix[i][j] == word[0]) | |
{ | |
var result = SearchFrom(matrix, word, i, j); | |
if (result.Count > 0) | |
{ | |
allResults.Add(result); | |
} | |
} | |
} | |
} | |
return allResults; | |
} | |
private static List<(int, int)> SearchFrom(char[][] matrix, string word, int x, int y) | |
{ | |
var rows = matrix.Length; | |
var cols = matrix[0].Length; | |
var positions = new List<(int, int)>(); | |
foreach (var (dx, dy) in Directions) | |
{ | |
for (var k = 0; k < word.Length; k++) | |
{ | |
var nx = x + dx * k; | |
var ny = y + dy * k; | |
if (nx < 0 || nx >= rows || ny < 0 || ny >= cols || matrix[nx][ny] != word[k]) | |
{ | |
positions.Clear(); | |
break; | |
} | |
positions.Add((nx, ny)); | |
} | |
if (positions.Count == word.Length) | |
{ | |
return positions; | |
} | |
} | |
return []; | |
} | |
private static string SolveWordSearch(char[][] matrix, List<string> words) | |
{ | |
var foundPositions = new HashSet<(int, int)>(); | |
foreach (var word in words) | |
{ | |
var results = SearchWord(matrix, word); | |
foreach (var result in results) | |
{ | |
foreach (var pos in result) | |
{ | |
foundPositions.Add(pos); | |
} | |
} | |
} | |
var remainingChars = new List<char>(); | |
for (var i = 0; i < matrix.Length; i++) | |
{ | |
for (var j = 0; j < matrix[i].Length; j++) | |
{ | |
if (!foundPositions.Contains((i, j))) | |
{ | |
remainingChars.Add(matrix[i][j]); | |
} | |
} | |
} | |
return string.Join(string.Empty, remainingChars); | |
} | |
static void Main() | |
{ | |
char[][] matrix = | |
{ | |
"KALTJSHODA".ToCharArray(), | |
"LLPUKLTOAT".ToCharArray(), | |
"AKTAAKAARR".ToCharArray(), | |
"SAANLAKPEA".ToCharArray(), | |
"ARPOVPTOKK".ToCharArray(), | |
"RHOMOLICEA".ToCharArray(), | |
"KOLSPEKESR".ToCharArray(), | |
"ORAOCAALTP".ToCharArray(), | |
"SPOKVSTIAA".ToCharArray(), | |
"MATKAFTKAT".ToCharArray(), | |
"AIAKOSTKAY".ToCharArray() | |
}; | |
var words = new List<string> | |
{ | |
"ALKA", "HORA", "JUTA", "KAPLE", "KARPATY", "KARTA", "KASA", "KAVKA", "KLAS", "KOSMONAUT", "KOST", "KROK", | |
"LAPKA", "MATKA", "OKRASA", "OPAT", "OSMA", "PAKT", "PATKA", "PIETA", "POCEL", "POVLAK", "PROHRA", "SEKERA", | |
"SHODA", "SOPKA", "TAKT", "TAKTIKA", "TLAK", "VOLHA" | |
}; | |
var solution = SolveWordSearch(matrix, words); | |
Console.WriteLine("Solution: " + solution); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment