public static bool FindPath(int size, int iEnd, int jEnd, Dictionary <string, Path> paths, HashSet <string> visited, UniqueQueue <string> notVisited ) { while (notVisited.Any()) { var currentNode = notVisited.Dequeue(); var ijCurrent = currentNode.Split('-').Select(x => int.Parse(x)).ToList(); var iStart = ijCurrent[0]; var jStart = ijCurrent[1]; if (iStart == iEnd && jStart == jEnd) { return(true); } //if (visited.Count >= size) //{ // ifImpossible = true; // return false; //} //if (iStart >= size || iStart < 0 // || jStart >= size || jStart < 0 //) //{ // return false; //} //var currentNode = $"{iStart}-{jStart}"; visited.Add(currentNode); if (!paths.ContainsKey(currentNode)) { paths.Add(currentNode, new Path()); } foreach (var move in _movesMap) { var newIStart = move.Value.Item1 + iStart; var newJStart = move.Value.Item2 + jStart; var newNode = $"{newIStart}-{newJStart}"; if (newIStart >= size || newIStart < 0 || newJStart >= size || newJStart < 0 || visited.Contains(newNode)) { continue; } if (!paths.ContainsKey(newNode)) { paths.Add(newNode, new Path()); } notVisited.Enqueue(newNode); var newCost = paths[currentNode].cost == int.MaxValue ? int.MaxValue : paths[currentNode].cost + 1; var cost = paths[newNode].cost; if (newCost < cost) { paths[newNode].cost = newCost; paths[newNode].PreviousNode = currentNode; paths[newNode].Move = move.Key; } } //foreach (var neighborNode in notVisited) //{ // var ij = neighborNode.Split('-').Select(x => int.Parse(x)).ToList(); // var isFound = FindPath(size, ij[0], ij[1], iEnd, jEnd, paths, visited, out ifImpossible); // if (isFound) // { // return true; // } // if (ifImpossible) // { // ifImpossible = true; // return false; // } //} } return(false); }