using System.Collections.Generic;
public char Value { get; private set; }
public int Line { get; private set; }
public int Column { get; private set; }
public bool IsEmpty { get; private set; }
public bool IsVowel { get; private set; }
public Cell(char value, int line, int column, HashSet<char> vowels)
IsVowel = vowels.Contains(value);
public override string ToString()
private static IReadOnlyCollection<(int Column, int Line)> FindMoves(Cell cell, int columnMin = 0, int columnMax = 4, int lineMin = 0, int lineMax = 4)
var moves = new List<(int Column, int Line)>(8);
if (cell.Column + 2 <= columnMax && cell.Line + 1 <= lineMax) {
moves.Add((cell.Column + 2, cell.Line + 1));
if (cell.Column + 1 <= columnMax && cell.Line + 2 <= lineMax) {
moves.Add((cell.Column + 1, cell.Line + 2));
if (cell.Column - 1 >= columnMin && cell.Line + 2 <= lineMax) {
moves.Add((cell.Column - 1, cell.Line + 2));
if (cell.Column - 2 >= columnMin && cell.Line + 1 <= lineMax) {
moves.Add((cell.Column - 2, cell.Line + 1));
if (cell.Column - 2 >= columnMin && cell.Line - 1 >= lineMin) {
moves.Add((cell.Column - 2, cell.Line - 1));
if (cell.Column - 1 >= columnMin && cell.Line - 2 >= lineMin) {
moves.Add((cell.Column - 1, cell.Line - 2));
if (cell.Column + 1 <= columnMax && cell.Line - 2 >= lineMin) {
moves.Add((cell.Column + 1, cell.Line - 2));
if (cell.Column + 2 <= columnMax && cell.Line - 1 >= lineMin) {
moves.Add((cell.Column + 2, cell.Line - 1));
public static void Main()
var vowels = new HashSet<char>() {'A', 'E', 'I', 'O', 'U', 'Y'};
{new Cell('A', 0, 0, vowels), new Cell('B', 0, 1, vowels), new Cell('C', 0, 2, vowels), new Cell(' ', 0, 3, vowels), new Cell('E', 0, 4, vowels)},
{new Cell(' ', 1, 0, vowels), new Cell('G', 1, 1, vowels), new Cell('H', 1, 2, vowels), new Cell('I', 1, 3, vowels), new Cell('J', 1, 4, vowels)},
{new Cell('K', 2, 0, vowels), new Cell('L', 2, 1, vowels), new Cell('M', 2, 2, vowels), new Cell('N', 2, 3, vowels), new Cell('O', 2, 4, vowels)},
{new Cell('P', 3, 0, vowels), new Cell('Q', 3, 1, vowels), new Cell('R', 3, 2, vowels), new Cell('S', 3, 3, vowels), new Cell('T', 3, 4, vowels)},
{new Cell('U', 4, 0, vowels), new Cell('V', 4, 1, vowels), new Cell(' ', 4, 2, vowels), new Cell(' ', 4, 3, vowels), new Cell('Y', 4, 4, vowels)}
const int vowelsMaxCount = 2;
const int pathMaxCount = 9;
var pathContexts = new Stack<(List<Cell> Path, int vowelsCount)>();
var pathes = new HashSet<string>();
foreach (var cell in cells) {
pathContexts.Push((new List<Cell>() {cell}, vowelsCount));
while (pathContexts.Count > 0) {
var pathContext = pathContexts.Pop();
if (pathContext.Path.Count >= pathMaxCount) {
pathes.Add(string.Join("", pathContext.Path));
foreach (var move in FindMoves(pathContext.Path[^1])) {
var cellTo = cells[move.Line, move.Column];
var newVowelsCount = pathContext.vowelsCount;
if (cellTo.IsVowel && ++newVowelsCount > vowelsMaxCount) {
var newPath = new List<Cell>(pathContext.Path);
pathContexts.Push((newPath, newVowelsCount));
Console.WriteLine(pathes.Count);