Find the number of occurrences of a given search word in a 2d-Array of characters where the word can go up, down, left, right and around 90 degree bends.
classSolution { int res, n, m; voidfloodfill(vector<vector<char>> &mat, string& t, int p, int y, int x){ if(p == t.length()) res++; else { if(0 <= y and y < n and0 <= x and x < m and mat[y][x] == t[p]) { mat[y][x] = '#'; floodfill(mat, t, p + 1, y - 1, x); floodfill(mat, t, p + 1, y + 1, x); floodfill(mat, t, p + 1, y, x + 1); floodfill(mat, t, p + 1, y, x - 1); mat[y][x] = t[p]; } } } public: intfindOccurrence(vector<vector<char>> &mat, string target){ res = 0, n = mat.size(), m = mat[0].size(); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { floodfill(mat, target, 0, i, j); } } return res / 4; } };