Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- first solution ie.commented is right,use this.
- we are using the grid as the visited array and when we push any point in queue,make it visted or in grid 0
- */
- bool check(vector <vector <int>> grid,int i,int j){
- if(i>=0 && j>=0 && i<grid.size() && j<grid[0].size() && grid[i][j]==1) return true;
- return false;
- }
- class Solution {
- public:
- int shortestPath(vector<vector<int>> &grid, pair<int, int> source,pair<int, int> des) {
- if(!check(grid,des.first,des.second) || !check(grid,source.first,source.second)) return -1;
- int dist=0;
- deque<pair<int,int>> q;
- q.push_back(source);
- grid[source.first][source.second]=0;
- int res=0;
- while(q.size()){
- int k=q.size();
- for(int i=0;i<k;i++){
- pair<int,int> temp=q.front();
- q.pop_front();
- if(temp.first==des.first && temp.second==des.second) return res;
- int row[] = {-1, 0, 0, 1};
- int col[] = {0, -1, 1, 0};
- for(int r=0;r<4;r++){
- if(check(grid,temp.first+row[r],temp.second+col[r])){
- q.push_back(make_pair(temp.first+row[r],temp.second+col[r]));
- grid[temp.first+row[r]][temp.second+col[r]]=0;
- }
- }
- }
- res++;
- }
- return -1;
- }
- };
- //second solution
- struct Node
- {
- // (x, y) represents matrix cell coordinates, and
- // `dist` represents their minimum distance from the source
- int x, y, dist;
- };
- // Below arrays detail all four possible movements from a cell
- int row[] = { -1, 0, 0, 1 };
- int col[] = { 0, -1, 1, 0 };
- // Function to check if it is possible to go to position (row, col)
- // from the current position. The function returns false if (row, col)
- // is not a valid position or has a value 0 or already visited.
- 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];
- }
- class Solution {
- public:
- int shortestPath(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;
- }
- // `M × N` matrix
- int M = mat.size();
- int N = mat[0].size();
- // construct a `M × N` matrix to keep track of visited cells
- vector<vector<bool>> visited;
- visited.resize(M, vector<bool>(N));
- // create an empty queue
- queue<Node> q;
- // get source cell (i, j)
- int i = src.first;
- int j = src.second;
- // mark the source cell as visited and enqueue the source node
- visited[i][j] = true;
- q.push({i, j, 0});
- // stores length of the longest path from source to destination
- int min_dist = INT_MAX;
- // loop till queue is empty
- while (!q.empty())
- {
- // dequeue front node and process it
- Node node = q.front();
- q.pop();
- // (i, j) represents a current cell, and `dist` stores its
- // minimum distance from the source
- int i = node.x, j = node.y, dist = node.dist;
- // if the destination is found, update `min_dist` and stop
- if (i == dest.first && j == dest.second)
- {
- min_dist = dist;
- break;
- }
- // check for all four possible movements from the current cell
- // and enqueue each valid movement
- for (int k = 0; k < 4; k++)
- {
- // check if it is possible to go to position
- // (i + row[k], j + col[k]) from current position
- if (isValid(mat, visited, i + row[k], j + col[k]))
- {
- // mark next cell as visited and enqueue it
- 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;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement