Advertisement
Hamoudi30

8D Check

Jan 5th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // 8-D check
  4. const int N = 100;
  5. char grid[N][N];
  6. bool vis[N][N];
  7. int n, m;
  8. bool valid (int row, int col) {
  9. return (row >= 0 && col >= 0 && row < n && col < m);
  10. }
  11. bool FindEnd (int row, int col) {
  12. // base case
  13. if (!valid(row, col) || grid[row][col] == 'X' || vis[row][col])
  14. return false;
  15.  
  16. if (grid[row][col] == 'E')
  17. return true;
  18. // recursive case
  19. vis[row][col] = true;
  20. // 8 directions
  21. if (FindEnd(row + 1, col)) // 1
  22. return true;
  23. if (FindEnd(row - 1, col)) // 2
  24. return true;
  25. if (FindEnd(row, col + 1)) // 3
  26. return true;
  27. if (FindEnd(row, col - 1)) // 4
  28. return true;
  29. if (FindEnd(row + 1, col + 1)) // 5
  30. return true;
  31. if (FindEnd(row - 1, col - 1)) // 6
  32. return true;
  33. if (FindEnd(row - 1, col + 1)) // 7
  34. return true;
  35. if (FindEnd(row + 1, col - 1)) // 8
  36. return true;
  37. // not found
  38. return false;
  39. }
  40.  
  41. int main() {
  42.  
  43. // freopen("/home/hamoudi/Coding/run.in", "r", stdin);
  44.  
  45. cin >> n >> m;
  46. int st_r = -1, st_c = -1;
  47. for (int i = 0; i < n; ++i) {
  48. for (int j = 0; j < m; ++j) {
  49. cin >> grid[i][j];
  50. if (grid[i][j] == 'S') {
  51. st_r = i;
  52. st_c = j;
  53. }
  54. }
  55. }
  56.  
  57. if (FindEnd(st_r, st_c)) cout << "found";
  58. else cout << "not";
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement