-
Notifications
You must be signed in to change notification settings - Fork 0
/
Knight in Geekland
70 lines (56 loc) · 1.73 KB
/
Knight in Geekland
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
59
60
61
62
63
64
65
66
67
68
69
70
class Solution{
public:
bool is_Safe(int i, int j, int r, int c) {
if(i < 0 || i >= r || j < 0 || j >= c) {
return false;
}
return true;
}
int knightInGeekland(int start_x,int start_y,vector<vector<int>> &arr){
// Code here
int r = arr.size();
int c =arr[0].size();
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
vector<vector<bool>> vis(r,vector<bool>(c));
vector<int> al;
queue<pair<int,int>> q;
q.push({start_x, start_y});
vis[start_x][start_y] = true;
while(!q.empty()) {
int size = q.size();
int curr_lev_pts = 0;
for(int i = 0; i < size; i++) {
pair<int,int> curr = q.front();
q.pop();
int x = curr.first;
int y = curr.second;
curr_lev_pts += arr[x][y];
//vis all its 8 moves;
for(int j = 0 ;j < 8; j++) {
int n_x = x + dx[j];
int n_y = y + dy[j];
if(is_Safe(n_x, n_y, r, c) && !vis[n_x][n_y]) {
vis[n_x][n_y] = true;
q.push({n_x, n_y});
}
}
}
al.push_back(curr_lev_pts);
}
int max = INT_MIN;
int ans = -1;
for(int i = al.size()-1; i>=0 ; i--) {
if(al[i] + i < al.size()) {
al[i] += al[i + al[i]];
}
}
for(int i = 0; i < al.size(); i++) {
if(al[i] > max) {
max = al[i];
ans = i;
}
}
return ans;
}
};