Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 8-D check
- const int N = 100;
- char a[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 can (int row, int col) {
- // base case
- if (!valid(row, col) || a[row][col] == 'X' || vis[row][col])
- return false;
- if (a[row][col] == 'E')
- return true;
- // recursive case
- vis[row][col] = true;
- // 8 directions
- if (can(row + 1, col)) // 1
- return true;
- if (can(row - 1, col)) // 2
- return true;
- if (can(row, col + 1)) // 3
- return true;
- if (can(row, col - 1)) // 4
- return true;
- if (can(row + 1, col + 1)) // 5
- return true;
- if (can(row - 1, col - 1)) // 6
- return true;
- if (can(row - 1, col + 1)) // 7
- return true;
- if (can(row + 1, col - 1)) // 8
- return true;
- // can't reach End
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement