Lesson 5
Path Traversal in 2D Arrays Using C#
Introduction

Hello, and welcome back to our coding lesson series. In this unit, we have a fascinating problem at hand that uses the concept of 2D arrays or grids. What's interesting about this problem is that it involves not only the simple traversal of the grid but also making this traversal in a unique manner. Initially, the concept might seem a bit tricky, but as we dissect the task and take it step by step, you're sure to enjoy how it unfolds. Are you ready to embark on this adventure? Let's dive right in!

Task Statement

The task before us involves the creation of a C# function named PathTraverse. This function should perform a particularly ordered traversal through a 2D grid. The function will accept a grid represented as a 2D array int[,], along with the starting cell coordinates as parameters. Starting from the provided cell, the function should move in any one of the four possible directions toward an adjacent cell. However, a condition governs this selection: the new cell value should be strictly greater than the current cell's value. Think of this as navigating a hiking trail where you can only move to higher ground from your current position.

This pattern would continue as we keep selecting the next available, larger cell value. The traversal would halt when there are no cells left that satisfy our criteria. The final result of the function will be a List<int> that includes all the visited cell values in the order of their visitation.

Consider a 3x3 grid:

Plain text
11 2 3 24 5 6 37 8 9

If we start at the cell with the value 5, we can logically move to either 6 or 8. Let's say we choose 8; the only cell that we can now move to is 9. After this, we have no more moves left that fit our criteria. Hence, the function returns {5, 8, 9}.

Solution Building: Step 1

The first thing we need to do is determine the dimensions of our grid, which is easily done in C# using the GetLength method. We can also establish the directions that our traversal can take. In the context of matrices, when we say we are moving "up," we are moving one step towards the first row (decreasing the row index). Similarly, moving "down" corresponds to moving one step towards the last row (increasing the row index), and moving left or right relates to decrementing or incrementing the column index, respectively.

C#
1using System; 2using System.Collections.Generic; 3 4class Solution { 5 public static List<int> PathTraverse(int[,] grid, int startRow, int startCol) { 6 int rows = grid.GetLength(0); 7 int cols = grid.GetLength(1); 8 int[,] directions = { 9 { -1, 0 }, // Up 10 { 1, 0 }, // Down 11 { 0, -1 }, // Left 12 { 0, 1 } // Right 13 };

In the directions array, each pair represents a direction in terms of a pair (rowOffset, colOffset). So, if we are at a cell (r, c), moving up corresponds to going to the cell (r-1, c), moving down corresponds to (r+1, c), moving left corresponds to (r, c-1), and moving right corresponds to (r, c+1).

Solution Building: Step 2

Once we have the grid's dimensions and the possible directions, we should validate the starting point and set up our visited cell recording mechanism.

C#
1using System; 2using System.Collections.Generic; 3 4class Solution { 5 public static List<int> PathTraverse(int[,] grid, int startRow, int startCol) { 6 int rows = grid.GetLength(0); 7 int cols = grid.GetLength(1); 8 9 // Check the validity of the input 10 if (startRow < 0 || startRow >= rows || startCol < 0 || startCol >= cols) { 11 throw new ArgumentException("Invalid input"); 12 } 13 14 // Define all four possible directions of movement 15 int[,] directions = { 16 {1, 0}, {-1, 0}, {0, -1}, {0, 1} 17 }; 18 19 // Start with the value at the starting cell 20 List<int> visited = new List<int>(); 21 visited.Add(grid[startRow, startCol]);
Solution Building: Step 3

Let's initiate the grid traversal process in our function. We first set up an infinite loop that will only stop when we break it based on a condition. Inside the infinite loop, for each iteration, we'll have the function try to select the next cell with the maximum value among the adjacent cells. If we find such a cell, we'll capture its value, and it will be our next cell.

C#
1using System; 2using System.Collections.Generic; 3 4class Solution { 5 public static List<int> PathTraverse(int[,] grid, int startRow, int startCol) { 6 int rows = grid.GetLength(0); 7 int cols = grid.GetLength(1); 8 9 // Check the validity of the input 10 if (startRow < 0 || startRow >= rows || startCol < 0 || startCol >= cols) { 11 throw new ArgumentException("Invalid input"); 12 } 13 14 // Define all four possible directions of movement 15 int[,] directions = { 16 {1, 0}, {-1, 0}, {0, -1}, {0, 1} 17 }; 18 19 // Start with the value at the starting cell 20 List<int> visited = new List<int>(); 21 visited.Add(grid[startRow, startCol]); 22 23 while (true) { 24 // Initialize the current maximum as negative one 25 int currMax = -1; 26 int nextRow = -1, nextCol = -1; 27 28 // Loop over each adjacent cell in all the directions 29 for (int i = 0; i < directions.GetLength(0); i++) { 30 // Calculate the new cell's row and column indices 31 int newRow = startRow + directions[i, 0]; 32 int newCol = startCol + directions[i, 1]; 33 34 // If the new cell is out of the grid boundary, ignore it 35 if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols) { 36 continue; 37 } 38 39 // If the new cell's value is greater than the current maximum 40 if (grid[newRow, newCol] > currMax) { 41 // Save it as the next cell to visit 42 nextRow = newRow; 43 nextCol = newCol; 44 currMax = grid[newRow, newCol]; 45 } 46 } 47 48 // If we don't have any valid cell to visit, break from the loop 49 if (currMax <= grid[startRow, startCol]) { 50 break; 51 } 52 53 // Otherwise, go to the next cell 54 startRow = nextRow; 55 startCol = nextCol; 56 57 // Append the cell's value to the result list 58 visited.Add(currMax); 59 } 60 61 // Return the list of visited cells' values 62 return visited; 63 } 64 65 public static void Main(string[] args) { 66 int[,] grid = { 67 {1, 2, 3}, 68 {4, 5, 6}, 69 {7, 8, 9} 70 }; 71 List<int> res = PathTraverse(grid, 1, 1); 72 foreach (int val in res) { 73 Console.Write(val + " "); 74 } 75 Console.WriteLine(); 76 77 // Outputs: 5 8 9 78 } 79}
Lesson Summary

Bravo! You've successfully solved a complex problem involving the traversal of a grid in a unique pattern. This function has tested not only your skills in C# programming but also your ability to visualize spatial patterns.

Having digested this knowledge, it's now time to test your understanding and apply these concepts to similar problems. Watch out for the ensuing practice session, where you can dabble with more challenging problems and refine your problem-solving skills. Keep up the good work and happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.