SHARE
TWEET

Untitled

a guest Sep 17th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, m;
  6. int ans[1000][1000];
  7. char arr[1000][1000];
  8.  
  9. void dfs(int posX, int posY, int prev)
  10. {
  11.     if(posX >= n || posY >= m || posX < 0 || posY < 0)
  12.         return;
  13.     if(arr[posX][posY] == '#')
  14.         return;
  15.     if(ans[posX][posY] < prev + 1)
  16.         return;
  17.     ans[posX][posY] = prev + 1;
  18.    
  19.     dfs(posX + 1, posY, prev + 1);
  20.     dfs(posX - 1, posY, prev + 1);
  21.     dfs(posX, posY + 1, prev + 1);
  22.     dfs(posX, posY - 1, prev + 1);
  23. }
  24.  
  25. int main()
  26. {
  27.     cin >> n >> m;
  28.     for(int i = 0; i < n; i++)
  29.         for(int j = 0; j < m; j++)
  30.         {
  31.             cin >> arr[i][j];
  32.             ans[i][j] = INT_MAX;
  33.         }
  34.     int aX, aY, bX, bY;
  35.     cin >> aX >> aY >> bX >> bY;
  36.     dfs(aX, aY, 0);
  37.     ans[bX][bY] == INT_MAX ? cout << 0 : cout << ans[bX][bY];
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top