Skip to content

Commit c09953e

Browse files
committed
Word Search
1 parent 8c38a79 commit c09953e

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

Leetcode_Practice_Questions/LC_1219.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@ int recursion(int i , int j, vector<vector<int> >&grid){
33
if(i<0 || j<0 || i == grid.size() || j == grid[0].size() || grid[i][j] == 0){
44
return 0;
55
}
6-
int current = grid[i][j];
7-
grid[i][j] = 0;
8-
int ans = current;
9-
int ans1 = 0;
10-
ans1 = max(ans1,recursion(i-1,j,grid));
11-
ans1 = max(ans1,recursion(i+1,j,grid));
12-
ans1 = max(ans1, recursion(i,j+1,grid));
13-
ans1 = max(ans1, recursion(i,j-1,grid));
14-
ans+=ans1;
15-
grid[i][j] = current;
16-
return ans;
6+
7+
int originalGold = grid[r][c];
8+
grid[r][c] = 0; // Mark as visited by taking the gold
9+
10+
// Explore all 4 directions and find the path with the maximum gold
11+
int maxGoldFromNeighbors = 0;
12+
maxGoldFromNeighbors = max(maxGoldFromNeighbors, dfs(r + 1, c, grid));
13+
maxGoldFromNeighbors = max(maxGoldFromNeighbors, dfs(r - 1, c, grid));
14+
maxGoldFromNeighbors = max(maxGoldFromNeighbors, dfs(r, c + 1, grid));
15+
maxGoldFromNeighbors = max(maxGoldFromNeighbors, dfs(r, c - 1, grid));
16+
17+
grid[r][c] = originalGold; // Backtrack: put the gold back for other paths
18+
return originalGold + maxGoldFromNeighbors;
1719
}
1820
int getMaximumGold(vector<vector<int>>& grid) {
1921
int ans = 0;
2022
for(int i = 0; i<grid.size(); i++){
2123
for(int j = 0; j<grid[0].size();j++){
2224
if(grid[i][j]!= 0){
23-
ans = max(ans,recursion(i,j,grid));
25+
ans = max(ans, dfs(i, j, grid));
2426
}
2527
}
2628
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<string>
4+
using namespace std;
5+
class Solution{
6+
public:
7+
int m;
8+
int n;
9+
vector<vector<char>> board;
10+
string word;
11+
bool backtrack(int i, int j ,int k){
12+
if(k == word.length()){
13+
return true;
14+
}
15+
if(i < 0 || i >= m || j < 0 || j >= n || board[i][j] != word[k]){
16+
return false;
17+
}
18+
char temp = board[i][j];
19+
board[i][j] = '\0';
20+
bool found = backtrack(i + 1, j, k + 1) || backtrack(i - 1, j , k + 1) || backtrack(i, j + 1, k + 1) || backtrack(i , j - 1, k + 1);
21+
board[i][j] = temp;
22+
return found;
23+
}
24+
bool exist(vector<vector<char>> board, string word){
25+
this-> m = board.size();
26+
this-> n = board[0].size();
27+
for(int i = 0 ; i < m; i++){
28+
for(int j = 0; j < n; j++){
29+
if(backtrack(i , j , 0)){
30+
return true;
31+
}
32+
}
33+
}
34+
return false;
35+
}
36+
};
37+
int main(){
38+
vector<vector<char>> board = {
39+
{'A', 'B', 'C', 'E'},
40+
{'S', 'F', 'C', 'S'},
41+
{'A', 'D', 'E', 'E'}
42+
};
43+
Solution sol;
44+
string word = "ABCCED";
45+
if(sol.exist(board, word)){
46+
cout << "The elements are present in the board" << endl;
47+
}
48+
else{
49+
cout << "The element are not present in the board" << endl;
50+
}
51+
return 0;
52+
}

0 commit comments

Comments
 (0)