Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Cell startingPosition = new Cell(Console.ReadLine().Split(' ').Select(int.Parse).ToArray());
            Cell mazeSize = new Cell(Console.ReadLine().Split(' ').Select(int.Parse).ToArray());
            matrix = new int[mazeSize.row, mazeSize.col];
            visited = new bool[mazeSize.row, mazeSize.col];
            for (int i = 0; i < mazeSize.row; i++)
            {
                string[] rowInput = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < rowInput.Length; j++)
                {
                    if (!rowInput[j].Contains('#'))
                    {
                        matrix[i, j] = int.Parse(rowInput[j]);
                    }
                    else
                    {
                        visited[i, j] = true;
                        //                        matrix[i, j] = -1;
                    }
                }
            }

            Recursion(startingPosition, 0);
            Console.WriteLine(maxSum);
        }
Exemplo n.º 2
0
        static void Recursion(Cell currentCell, int sumSoFar)
        {
            if (!InCell(currentCell))
            {
                return;
            }

            if (visited[currentCell.row, currentCell.col])
            {
                sumSoFar += GetCellValue(currentCell);
            //                Console.WriteLine("->" + sumSoFar);

                if (sumSoFar > maxSum)
                {
                    maxSum = sumSoFar;
                }
                return;
            }

            if (InCell(new Cell(currentCell.row + GetCellValue(currentCell), currentCell.col)) ||
                InCell(new Cell(currentCell.row - GetCellValue(currentCell), currentCell.col)) ||
                InCell(new Cell(currentCell.row, currentCell.col + GetCellValue(currentCell))) ||
                InCell(new Cell(currentCell.row, currentCell.col - GetCellValue(currentCell))))
            {
                sumSoFar += GetCellValue(currentCell);
                visited[currentCell.row, currentCell.col] = true;
                Recursion(new Cell(currentCell.row + GetCellValue(currentCell), currentCell.col), sumSoFar);
                Recursion(new Cell(currentCell.row - GetCellValue(currentCell), currentCell.col), sumSoFar);
                Recursion(new Cell(currentCell.row, currentCell.col + GetCellValue(currentCell)), sumSoFar);
                Recursion(new Cell(currentCell.row, currentCell.col - GetCellValue(currentCell)), sumSoFar);
            }
        }
Exemplo n.º 3
0
        static void Recursion(Cell currentCell, int sumSoFar)
        {
            if (!InCell(currentCell) || visited[currentCell.row, currentCell.col])
            {
                if (sumSoFar > maxSum)
                {
                    maxSum = sumSoFar;
                }
                return;
            }

            sumSoFar += GetCellValue(currentCell);
            visited[currentCell.row, currentCell.col] = true;
            Recursion(new Cell(currentCell.row + GetCellValue(currentCell), currentCell.col), sumSoFar);
            Recursion(new Cell(currentCell.row - GetCellValue(currentCell), currentCell.col), sumSoFar);
            Recursion(new Cell(currentCell.row, currentCell.col + GetCellValue(currentCell)), sumSoFar);
            Recursion(new Cell(currentCell.row, currentCell.col - GetCellValue(currentCell)), sumSoFar);
            sumSoFar -= GetCellValue(currentCell);
            visited[currentCell.row, currentCell.col] = false;
        }
Exemplo n.º 4
0
 static bool InCell(Cell cell)
 {
     return cell.row > -1 && cell.row < matrix.GetLength(0) &&
         cell.col > -1 && cell.col < matrix.GetLength(1);
 }
Exemplo n.º 5
0
 static int GetCellValue(Cell cell)
 {
     return matrix[cell.row, cell.col];
 }