siddharth963

Breadth first search shortest path code

Aug 16th, 2020 (edited)
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. queue<pair<int,int>>Q;
  5. char arr[10000][10000];
  6. int visited[10000][10000];
  7. int visited2[10000][10000];
  8.  
  9. void solve(int n,int m,int startr,int startc,int endr,int endc){
  10.     // int neighbor[][2]={{0,-1},{1,0},{0,1},{-1,0}};
  11.     vector<pair<int,int>>neighbor={{0,-1},{1,0},{0,1},{-1,0}};
  12.     Q.push({startr,startc});
  13.     bool reached=false;
  14.     while(!Q.empty()){
  15.         for(int i=0;i<4;i++){
  16.             int x=Q.front().first+neighbor[i].first,y=Q.front().second+neighbor[i].second;
  17.             if(1<=x && x<=n && 1<=y && y<=m){
  18.                 if(arr[x][y]!='#' && (!(visited[x][y]))){
  19.                     visited[x][y]=Q.front().first; visited2[x][y]=Q.front().second;
  20.                     Q.push({x,y});
  21.                 }
  22.             }
  23.         }
  24.         if(Q.front().first==endr && Q.front().second==endc){
  25.             printf("reached\n");reached=true; break;// goto reached;
  26.         }
  27.         Q.pop();
  28.     }
  29.     if(!reached){
  30.         cout<<"Not reached\n"; return;
  31.     }
  32.     visited[1][1]=1; visited2[1][1]=1;
  33.     for(int i=1;i<=n;i++){
  34.         for(int j=1;j<=m;j++){
  35.             cout<<"("<<visited[i][j]<<","<<visited2[i][j]<<") ";
  36.         }
  37.         cout<<endl;
  38.     }
  39.     // reached:
  40.     vector<pair<int,int>>path;
  41.     path.push_back({endr,endc});
  42.     while(endr!=startr || endc!=startc){
  43.         int endrr=endr,endcc=endc;
  44.         path.push_back({visited[endr][endc],visited2[endr][endc]});
  45.         endr=visited[endrr][endcc];   endc=visited2[endrr][endcc] ;
  46.     }
  47.     reverse(path.begin(),path.end());
  48.     cout<<"Number cells visited in the shortest path is: "<<path.size()-2<<endl;
  49.     for(int i=0;i<path.size();i++){
  50.         cout<<path[i].first<<" "<<path[i].second<<endl;
  51.     }
  52.     return;
  53. }
  54.  
  55. int main(){
  56.     int n,m;
  57.     cin>>n>>m;  // size of  matrix
  58.     string str;
  59.     for(int i=1;i<=n;i++){
  60.         cin>>str;
  61.         for(int j=1;j<=m;j++){
  62.             arr[i][j]=str[j-1];
  63.         }
  64.     }
  65.     int startr,startc,endr,endc;
  66.     cin>>startr>>startc>>endr>>endc;
  67.     solve(n,m,startr,startc,endr,endc);  // enter start_row,start_column, end_row,end_column
  68.     return 0;
  69. }
  70. // youtube link -> https://www.youtube.com/watch?v=KiCBXu4P-2Y&list=PLDV1Zeh2NRsDGO4--qE8yH72HFL1Km93P&index=6
  71. /*About reconstruction the path
  72. To reconstruct the path from the start to the end you need to maintain additional information, namely a 2D matrix which tracks the cell which was used to reach the current cell. Let me call this matrix "prev", short for previous. Every time you advance to the next cell, keep track of which cell you came from in the prev matrix. Once you reach the end of the BFS, start reconstructing the path by beginning at the end node in the prev matrix and work your way to the start node. The obtained path will be in reverse order, so you will need to reverse it.
  73. */
  74. /*Some inputs
  75. // all the below cells have valid paths
  76. 2 2
  77. 00
  78. #0
  79. 1 1 2 2
  80.  
  81. 5 5
  82. 00#0#
  83. #0##0
  84. 00000
  85. ###0#
  86. 0000#
  87. 1 1 5 1
  88.  
  89. 6 7
  90. 0#00000
  91. 000#0#0
  92. 00#0000
  93. 0#000#0
  94. #0#0#00
  95. 000000#
  96. 4 1 6 1
  97.  
  98. 10 10
  99. .#.......#
  100. .#......#.
  101. ...#.#....
  102. ...#...#..
  103. .#..###..#
  104. ...#...#..
  105. #...#....#
  106. ..#...#.#.
  107. ...####...
  108. .#......#.
  109. 1 1 10 6
  110.  
  111. */
Add Comment
Please, Sign In to add comment