rafid_shad

DFS on Grid

Jan 21st, 2021 (edited)
577
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define loop(i, n)          for(int i = 0; i < n; i++)
  5.  
  6. ///----------------------graph moves----------------*/
  7. int dr[] = {+1, -1, +0, +0};
  8. int dc[] = {+0, +0, +1, -1};
  9.  
  10. const int N = 100;
  11. int n;
  12. int grid[N][N];
  13. int vis[N][N];
  14.  
  15. void dfs(int r, int c){
  16.     vis[r][c] = 1;
  17.     loop(k, 4){
  18.         int vr = r + dr[k];
  19.         int vc = c + dc[k];
  20.         if(vr == n or vr < 0 or vc == n or vc < 0) continue;
  21.         if(!vis[vr][vc]) dfs(vr, vc);
  22.     }
  23. }
  24.  
  25. int main()
  26. {
  27.     #ifndef ONLINE_JUDGE
  28.    // read();
  29.     #endif // ONLINE_JUDGE
  30.    //fastIO;
  31.    int n;
  32.    cin >> n;
  33.  
  34.    dfs(0, 0);
  35.  
  36.  
  37. }
  38.  
  39.  
  40.  
RAW Paste Data