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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include "library.h"
#include <iostream> #include <queue> #include <vector>
using namespace std;
struct Node { int x, y, dist; };
bool isValid(vector<vector<int>> const &mat, vector<vector<bool>> &visited, int row, int col) { return (row >= 0 && row < mat.size()) && (col >= 0 && col < mat[0].size()) && mat[row][col] && !visited[row][col]; }
int findShortestPathLength(vector<vector<int>> &mat, pair<int, int> &src, pair<int, int> &dest) { if (mat.size() == 0 || mat[src.first][src.second] == 0 || mat[dest.first][dest.second] == 0) { return -1; }
int M = mat.size(); int N = mat[0].size();
int row[] = {-1, 0, 0, 1}; int col[] = {0, -1, 1, 0};
queue<Node> q; int i = src.first; int j = src.second;
vector<vector<bool>> visited; visited.resize(M, vector<bool>(N));
visited[i][j] = true; q.push({i, j, 0});
int min_dist = INT_MAX;
while (!q.empty()) { Node node = q.front(); q.pop();
int i = node.x, j = node.y, dist = node.dist;
if (i == dest.first && j == dest.second) { min_dist = dist; break; }
for (int k = 0; k < 4; k++) { if (isValid(mat, visited, i + row[k], j + col[k])) { visited[i + row[k]][j + col[k]] = true; q.push({ i + row[k], j + col[k], dist + 1 }); } } }
if (min_dist != INT_MAX) { return min_dist; }
return -1; }
int main() { vector<vector<int>> mat = { { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 }, { 0, 1, 1, 1, 1, 1, 0, 1, 0, 1 }, { 0, 0, 1, 0, 1, 1, 1, 0, 0, 1 }, { 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 }, { 0, 0, 0, 1, 0, 0, 0, 1, 0, 1 }, { 1, 0, 1, 1, 1, 0, 0, 1, 1, 0 }, { 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 }, { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 }, { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 }, { 0, 0, 1, 0, 0, 1, 1, 0, 0, 1 }, };
pair<int, int> src = make_pair(0, 0); pair<int, int> dest = make_pair(1, 2);
int min_dist = findShortestPathLength(mat, src, dest); if (min_dist != -1) { cout << "The shortest path from source to destination " "has length " << min_dist; } else { cout << "Destination cannot be reached from a given source"; }
return 0; }
|