-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG_Knight in Geekland.cpp
58 lines (49 loc) · 1.92 KB
/
GFG_Knight in Geekland.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution{
public:
int knightInGeekland(int start_x,int start_y,vector<vector<int>> &arr){
int n = arr.size(), m = arr[0].size();
vector<int>drx {2, 1, -1, -2, -2, -1, 1, 2};
vector<int>dry {1, 2, 2, 1, -1, -2, -2, -1 };
vector<int>coins;
vector<vector<int>> vis(n, vector<int> (m, 0));
queue<pair<int, int>> q;
q.push({start_x, start_y});
vis[start_x][start_y] = 1;
// Applying BFS - As we have to work on each level it is more beneficial to use three loops
while(!q.empty()){
int levelSize = q.size();
int stepCoins = 0;
// Processing a level
for(int i = 0 ; i < levelSize ; i++){
// Fetching details
auto it = q.front();
int x = it.first;
int y = it.second;
stepCoins += arr[x][y];
q.pop();
// Check in all 8 direction a knight can move
for(int i = 0 ; i < 8 ; i++){
int newRow = x + drx[i];
int newCol = y + dry[i];
if(newRow >= 0 && newCol >= 0 && newRow < n && newCol < m && !vis[newRow][newCol]){
vis[newRow][newCol] = 1;
q.push({newRow, newCol});
}
}
}
// Level finished
coins.push_back(stepCoins);
}
// Coins array is prepared
int ans = -1, csize = coins.size(), currMax = INT_MIN;
for(int i = csize - 1 ; i >= 0 ; i--){
if(i + coins[i] < csize)
coins[i] += coins[i + coins[i]];
if(currMax <= coins[i]){
ans = i;
currMax = coins[i];
}
}
return ans;
}
};