Advertisement
imashutosh51

Shortest distance in binary maze

Jul 31st, 2022 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. /*
  2. first solution ie.commented is right,use this.
  3. we are using the grid as the visited array and when we push any point in queue,make it visted or in grid 0
  4. */
  5.  bool check(vector <vector <int>> grid,int i,int j){
  6.      if(i>=0 && j>=0 && i<grid.size() && j<grid[0].size() && grid[i][j]==1) return true;
  7.      return false;
  8.  }
  9. class Solution {
  10.   public:
  11.     int shortestPath(vector<vector<int>> &grid, pair<int, int> source,pair<int, int> des) {
  12.         if(!check(grid,des.first,des.second) || !check(grid,source.first,source.second)) return -1;
  13.         int dist=0;
  14.         deque<pair<int,int>> q;
  15.         q.push_back(source);
  16.         grid[source.first][source.second]=0;
  17.         int res=0;
  18.         while(q.size()){
  19.             int k=q.size();
  20.             for(int i=0;i<k;i++){
  21.                 pair<int,int> temp=q.front();
  22.                 q.pop_front();
  23.                 if(temp.first==des.first && temp.second==des.second) return res;
  24.                 int row[] = {-1, 0, 0, 1};
  25.                 int col[] = {0, -1, 1, 0};
  26.                
  27.                 for(int r=0;r<4;r++){
  28.                     if(check(grid,temp.first+row[r],temp.second+col[r])){
  29.                         q.push_back(make_pair(temp.first+row[r],temp.second+col[r]));
  30.                         grid[temp.first+row[r]][temp.second+col[r]]=0;
  31.                     }
  32.                 }
  33.             }
  34.             res++;
  35.         }
  36.     return -1;
  37.     }
  38. };
  39. //second solution
  40. struct Node
  41. {
  42.     // (x, y) represents matrix cell coordinates, and
  43.     // `dist` represents their minimum distance from the source
  44.     int x, y, dist;
  45. };
  46.  
  47. // Below arrays detail all four possible movements from a cell
  48. int row[] = { -1, 0, 0, 1 };
  49. int col[] = { 0, -1, 1, 0 };
  50.  
  51. // Function to check if it is possible to go to position (row, col)
  52. // from the current position. The function returns false if (row, col)
  53. // is not a valid position or has a value 0 or already visited.
  54. bool isValid(vector<vector<int>> const &mat, vector<vector<bool>> &visited,
  55.         int row, int col) {
  56.     return (row >= 0 && row < mat.size()) && (col >= 0 && col < mat[0].size())
  57.         && mat[row][col] && !visited[row][col];
  58. }
  59.  
  60. class Solution {
  61.   public:
  62.     int shortestPath(vector<vector<int>> &mat, pair<int, int> src,pair<int, int> dest) {
  63.         if (mat.size() == 0 || mat[src.first][src.second] == 0 ||
  64.             mat[dest.first][dest.second] == 0) {
  65.         return -1;
  66.     }
  67.  
  68.     // `M × N` matrix
  69.     int M = mat.size();
  70.     int N = mat[0].size();
  71.  
  72.     // construct a `M × N` matrix to keep track of visited cells
  73.     vector<vector<bool>> visited;
  74.     visited.resize(M, vector<bool>(N));
  75.  
  76.     // create an empty queue
  77.     queue<Node> q;
  78.    
  79.     // get source cell (i, j)
  80.     int i = src.first;
  81.     int j = src.second;
  82.  
  83.     // mark the source cell as visited and enqueue the source node
  84.     visited[i][j] = true;
  85.     q.push({i, j, 0});
  86.  
  87.     // stores length of the longest path from source to destination
  88.     int min_dist = INT_MAX;
  89.  
  90.     // loop till queue is empty
  91.     while (!q.empty())
  92.     {
  93.         // dequeue front node and process it
  94.         Node node = q.front();
  95.         q.pop();
  96.  
  97.         // (i, j) represents a current cell, and `dist` stores its
  98.         // minimum distance from the source
  99.         int i = node.x, j = node.y, dist = node.dist;
  100.  
  101.         // if the destination is found, update `min_dist` and stop
  102.         if (i == dest.first && j == dest.second)
  103.         {
  104.             min_dist = dist;
  105.             break;
  106.         }
  107.  
  108.         // check for all four possible movements from the current cell
  109.         // and enqueue each valid movement
  110.         for (int k = 0; k < 4; k++)
  111.         {
  112.             // check if it is possible to go to position
  113.             // (i + row[k], j + col[k]) from current position
  114.             if (isValid(mat, visited, i + row[k], j + col[k]))
  115.             {
  116.                 // mark next cell as visited and enqueue it
  117.                 visited[i + row[k]][j + col[k]] = true;
  118.                 q.push({ i + row[k], j + col[k], dist + 1 });
  119.             }
  120.         }
  121.     }
  122.  
  123.     if (min_dist != INT_MAX) {
  124.         return min_dist;
  125.     }
  126.  
  127.     return -1;
  128.     }
  129. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement