C# || How To Find The Shortest Clear Path In A Binary Matrix Using C#
The following is a module with functions which demonstrates how to find the shortest clear path in a binary matrix using C#.
1. Shortest Path Binary Matrix – Problem Statement
Given an n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1.
A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0)) to the bottom-right cell (i.e., (n – 1, n – 1)) such that:
- All the visited cells of the path are 0.
- All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner).
The length of a clear path is the number of visited cells of this path.
Example 1:
Input: grid = [[0,1],[1,0]]
Output: 2
Example 2:
Input: grid = [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,0]]
Output: -1
2. Shortest Path Binary Matrix – Solution
The following is a solution which demonstrates how find the shortest clear path in a binary matrix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jun 3, 2022 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to find shortest path binary matrix // ============================================================================ public class Solution { // Search directions List<KeyValuePair<int, int>> directions = new List<KeyValuePair<int, int>>() { // Direction coordinates: (y, x) new KeyValuePair<int, int>(0, -1), new KeyValuePair<int, int>(0, 1), new KeyValuePair<int, int>(-1, 0), new KeyValuePair<int, int>(1, 0), new KeyValuePair<int, int>(1, 1), new KeyValuePair<int, int>(-1, -1), new KeyValuePair<int, int>(-1, 1), new KeyValuePair<int, int>(1, -1) }; public int ShortestPathBinaryMatrix(int[][] grid) { var NOT_VISITED = 0; var VISITED = 1; if (grid[0][0] == VISITED) { return -1; } var queue = new Queue<KeyValuePair<int, int>>(); queue.Enqueue(new KeyValuePair<int, int>(0, 0)); grid[0][0] = VISITED; var result = 0; while(queue.Count > 0) { ++result; for (var itemCount = queue.Count; itemCount > 0; --itemCount) { var current = queue.Dequeue(); // Get the current coordinates var row = current.Key; var col = current.Value; if (row == grid.Length - 1 && col == grid[row].Length - 1) { return result; } // Search in different directions foreach (var direction in directions) { var newRow = row + direction.Key; var newCol = col + direction.Value; if (IsValidCell(grid, newRow, newCol) && grid[newRow][newCol] == NOT_VISITED) { grid[newRow][newCol] = VISITED; queue.Enqueue(new KeyValuePair<int, int>(newRow, newCol)); } } } } return -1; } private bool IsValidCell(int[][] grid, int row, int col) { return row >= 0 && row < grid.Length && col >= 0 && col < grid[row].Length; } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output for the example cases:
2
4
-1
Leave a Reply