|
| 1 | +/* |
| 2 | + * Given a matrix with cell values 0 or 1. Find the length of the shortest path from (a1, b1) |
| 3 | + * to (a2, b2), such that: |
| 4 | + * Path can only be constructed through cells which have value 1. |
| 5 | + * You can only travel in 4 possible directions, i.e. left, right, up and down. |
| 6 | + * |
| 7 | + * For example: Given matrix: |
| 8 | + * Input: |
| 9 | + * mat[ROW][COL] = {{1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, |
| 10 | + * {1, 0, 1, 0, 1, 1, 1, 0, 1, 1 }, |
| 11 | + * {1, 1, 1, 0, 1, 1, 0, 1, 0, 1 }, |
| 12 | + * {0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, |
| 13 | + * {1, 1, 1, 0, 1, 1, 1, 0, 1, 0 }, |
| 14 | + * {1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }, |
| 15 | + * {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
| 16 | + * {1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, |
| 17 | + * {1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }}; |
| 18 | + * Source = {0, 0}; |
| 19 | + * Destination = {3, 4}; |
| 20 | + * Output: |
| 21 | + * Shortest Path is 11 |
| 22 | + * |
| 23 | + * We will use breadth first search, we start from source cell (and distance 0) |
| 24 | + * and explore neighbors in all possible directions, and keep adding |
| 25 | + * distance from source node for each cell, so for a cell we reach, |
| 26 | + * its shortest path = shortest path of parent + 1. We stop when we have reached |
| 27 | + * destination. If we have explored all possible valid cells from source cell, |
| 28 | + * we return false, i.e. we do not have valid path from source. |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +#include <iostream> |
| 33 | +#include <queue> |
| 34 | +#include <limits> |
| 35 | + |
| 36 | +struct Point |
| 37 | +{ |
| 38 | + int x; |
| 39 | + int y; |
| 40 | +}; |
| 41 | + |
| 42 | +struct Node |
| 43 | +{ |
| 44 | + Point point; |
| 45 | + int distance; |
| 46 | +}; |
| 47 | + |
| 48 | +bool valid(const std::vector<std::vector<int>>& matrix, int x, int y) |
| 49 | +{ |
| 50 | + return (x >= 0 && x < matrix[0].size() && y >= 0 && y < matrix.size()); |
| 51 | +} |
| 52 | + |
| 53 | +int shortestPath(const std::vector<std::vector<int>>& matrix, |
| 54 | + const Point& source, |
| 55 | + const Point& destination) |
| 56 | +{ |
| 57 | + // An auxillary matrix to keep track of visited points |
| 58 | + // initially all cells are marked unvisited. |
| 59 | + // |
| 60 | + std::vector<std::vector<bool>> visited( |
| 61 | + matrix.size(), |
| 62 | + std::vector<bool>(matrix[0].size(), false)); |
| 63 | + |
| 64 | + // Possible moves from a cell. |
| 65 | + // |
| 66 | + std::vector<int> row = {-1, 0, 0, 1}; |
| 67 | + std::vector<int> col = {0, -1, 1, 0}; |
| 68 | + |
| 69 | + std::queue<Node> nodeQueue; |
| 70 | + |
| 71 | + // mark the source cell visited and push it to queue. |
| 72 | + // |
| 73 | + visited[source.x][source.y] = true; |
| 74 | + nodeQueue.push({source.x, source.y, 0}); |
| 75 | + |
| 76 | + while (!nodeQueue.empty()) |
| 77 | + { |
| 78 | + // pop the front of the queue. |
| 79 | + Node current = nodeQueue.front(); |
| 80 | + nodeQueue.pop(); |
| 81 | + |
| 82 | + Point point = current.point; |
| 83 | + |
| 84 | + // if we have reached destination return distance. |
| 85 | + if (point.x == destination.x && point.y == destination.y) |
| 86 | + { |
| 87 | + return current.distance; |
| 88 | + } |
| 89 | + |
| 90 | + for (int i = 0; i < 4; ++i) |
| 91 | + { |
| 92 | + int r = point.x + row[i]; |
| 93 | + int c = point.y + col[i]; |
| 94 | + |
| 95 | + if (valid(matrix, r, c) && |
| 96 | + matrix[r][c] && !visited[r][c]) |
| 97 | + { |
| 98 | + visited[r][c] = true; |
| 99 | + Node adjNode = {r, c, current.distance + 1}; |
| 100 | + nodeQueue.push(adjNode); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return std::numeric_limits<int>::max(); |
| 105 | +} |
| 106 | + |
| 107 | +int main() |
| 108 | +{ |
| 109 | + const std::vector<std::vector<int>> matrix = |
| 110 | + {{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, |
| 111 | + { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 }, |
| 112 | + { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 }, |
| 113 | + { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, |
| 114 | + { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 }, |
| 115 | + { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }, |
| 116 | + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
| 117 | + { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, |
| 118 | + { 1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }}; |
| 119 | + |
| 120 | + Point source = {0, 0}; |
| 121 | + Point destination = {3, 4}; |
| 122 | + int distance = shortestPath(matrix, source, destination); |
| 123 | + if (distance != |
| 124 | + std::numeric_limits<int>::max()) |
| 125 | + { |
| 126 | + std::cout << "The distance between (" |
| 127 | + << source.x << ", " << source.y |
| 128 | + << ") and destination (" << destination.x |
| 129 | + << ", " << destination.y << ") is " |
| 130 | + << distance << std::endl; |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + std::cout << "The path does not exist between (" |
| 135 | + << source.x << ", " << source.y |
| 136 | + << ") and destination (" << destination.x |
| 137 | + << ", " << destination.y << ") is " |
| 138 | + << distance << std::endl; |
| 139 | + } |
| 140 | + return 0; |
| 141 | +} |
0 commit comments