Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- // 8-D check
- const int N = 100;
- char grid[N][N];
- bool vis[N][N];
- int n, m;
- bool valid (int row, int col) {
- return (row >= 0 && col >= 0 && row < n && col < m);
- }
- bool FindEnd (int row, int col) {
- // base case
- if (!valid(row, col) || grid[row][col] == 'X' || vis[row][col])
- return false;
- if (grid[row][col] == 'E')
- return true;
- // recursive case
- vis[row][col] = true;
- // 8 directions
- if (FindEnd(row + 1, col)) // 1
- return true;
- if (FindEnd(row - 1, col)) // 2
- return true;
- if (FindEnd(row, col + 1)) // 3
- return true;
- if (FindEnd(row, col - 1)) // 4
- return true;
- if (FindEnd(row + 1, col + 1)) // 5
- return true;
- if (FindEnd(row - 1, col - 1)) // 6
- return true;
- if (FindEnd(row - 1, col + 1)) // 7
- return true;
- if (FindEnd(row + 1, col - 1)) // 8
- return true;
- // not found
- return false;
- }
- int main() {
- // freopen("/home/hamoudi/Coding/run.in", "r", stdin);
- cin >> n >> m;
- int st_r = -1, st_c = -1;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cin >> grid[i][j];
- if (grid[i][j] == 'S') {
- st_r = i;
- st_c = j;
- }
- }
- }
- if (FindEnd(st_r, st_c)) cout << "found";
- else cout << "not";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement